Download it **here!**. NOTE: file contains macros (vba)

They are such a pain and cause so many problems. …they’re also a bit intriguing.

This VBA turns range ‘MergedCellRange’ into a merged cell:

MergedCellRange.Activate

With Selection

.MergeCells = True

End With

* Why create more?* I had to simulate what your work colleagues do to you.

Have you played Whack-A-Mole? That’s the concept I’ve used. Your task is to whack (click) merged cells without clicking cells that aren’t merged. Click the mallet button to add merged cells.

Gain points for whacking merged cells. Lose points for whacking non merged cells.

It becomes a memory game trying to remember where the new merged cells are located.

If you keep clicking the mallet without whacking any cells then my code misbehaves something about merged cells merging with other merged cells maybe?

Yes it’s lame but it gave me the chance to practice vba. It includes set, loop, event code, rnd(), etc.

**Set**

This creates a merged cell with some size variation (full code ‘Create Merged Cells’ further down).

Set MergedCellRange = Range(Cells(randr1, randc1), Cells(randr2, randc2))

**Loop**

A For Next loop to create up to 3 merged cells each time you click the mallet.

For X = 1 To LoopC

**Event Code**

Includes a toggle switch. When named range EventCodeSwitch is set to “yes” the code is turned off.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

‘TOGGLE THIS EVENT CODE OFF/ON

If Range(“EventCodeSwitch”).Value = “yes” Then Exit Sub

‘IF CLICKED CELL OUTSIDE RANGE THEN EXIT

If ActiveCell.Column > 46 Then Exit Sub

If ActiveCell.Row > 42 Then Exit Sub

‘IF CELL IS MERGED THEN…

If ActiveCell.MergeCells Then

‘UNMERGES EACH SELECTED MERGED CELL

With Selection

.MergeCells = False

End With

‘INCREASE SCORE: Merged Cells Clicked

Range(“BA20”).Value = Range(“BA20”).Value + 1

Else

‘INCREASE SCORE: Unmerged Cells Clicked

Range(“BA25”).Value = Range(“BA25”).Value + 1

End If

End Sub

**Create Merged Cells**

And finally the code to create the merged cells.

Sub CREATEMERGEDCELL()

Dim randr1 As Integer, randc1 As Integer, randr2 As Integer, randc2 As Integer, randx As Integer

Dim MergedCellRange As Range, actM As Range, LoopC As Integer

LoopC = 3

Application.ScreenUpdating = False

Range(“BA16”).Value = Range(“BA16”).Value + LoopC

For X = 1 To LoopC

‘Assign random values to the variables

randx1 = Int((5 – 1 + 1) * Rnd + 1)

randx2 = Int((4 – 1 + 1) * Rnd + 1)

randc1 = Int((34 – 1 + 1) * Rnd + 1)

randr1 = Int((37 – 1 + 1) * Rnd + 1)

randc2 = randc1 + randx1

randr2 = randr1 + randx2

‘CREATE MERGED CELL RANGE

Set MergedCellRange = Range(Cells(randr1, randc1), Cells(randr2, randc2))

‘SELECT AND MERGE CELLS

MergedCellRange.Activate

With Selection

.MergeCells = True

End With

MergedCellRange.Interior.Color = RGB(Int(Rnd() * 150), Int(Rnd() * 150), Int(Rnd() * 150))

Next X

Range(“BA25”).Value = Range(“BA25”).Value – LoopC

Range(“BA42”).Activate

Application.ScreenUpdating = True

End Sub

Most Excel fans will spend more time playing with the code as that’s more fun!

I’m a Data Analyst living in Markham Ontario Canada.

Microsoft Excel is still my favorite software despite all the new software out there these days.

There’s always something new to learn or old to practice in Excel.

The post whack-a-merged-cell first appeared on My Spreadsheet Lab.]]>

Here’s the link to Robert’s shortest formula challenge.

I initially struggled to understand it. Maybe that’s part of the challenge! On my 3rd attempt I got it!

**Raw Data**

Three columns of raw data in columns A, B, and C. 119 rows of data.

**Multi-step Solution**

Starting in cell E1 we see 8 different matrices. Various combinations of Products (column A) and Variants (column B). Let’s focus on the top matrix (challenge #1).

- SUMIFS, range F3:J6, sum column C based on product and variant conditions
- Column K has a total for each row
- Cell K7 MAX function displays max value from totals above
- “P4” has max total row of 747000. Cell E7 lookup function = “P4” (max total of items P1 P2 P3 P4)

**The Challenge**

Scroll down to row 70 to see challenge #1.

Cell J70 shows answer “P4” by referencing cell E7 that we just saw. **Here’s the challenge:**

For challenge #1 get answer “P4” using a single formula!

I’m won’t attempt to solve this. My victory is understanding Robert’s solution!

Cell K70 has Robert’s formula solution for challenge #1:

=LOOKUP(2,

1/FREQUENCY(0,1/(1+MMULT(SUMIFS(C$2:C$120,

A$2:A$120,”P”&ROW($1:$4),B$2:B$120,”V”&COLUMN(A:E)),

ROW($1:$5)^0))),

“P”&ROW($1:$4))

LOOKUP function has 3 parts. Part 1 and part 3 are simple. Part 2, lookup_vector, is the beast!

LOOKUP(lookup_value, lookup_vector, [result_vector])

**Sneaky Concept**

lookup_value of 2 will never be found in the lookup_vector. Huh? I’ll explain:

I’ve evaluated (using F9 key) lookup_vector and [result_vector]. Results are:

=LOOKUP(2,

{#DIV/0!;#DIV/0!;#DIV/0!;1;#DIV/0!},

{“P1″;”P2″;”P3″;”P4”})

2 isn’t found in the lookup_vector . 1 is the only number in lookup_vector.

If the only number is equal to or less than the lookup_value the function can return the [result_vector] answer. As the lookup_vector 1 is in the 4th position the [result_vector] gives us “P4”.

Let’s modify it a bit:

=LOOKUP(2,

{1;#DIV/0!;#DIV/0!;#DIV/0!;#DIV/0!},

{“P1″;”P2″;”P3″;”P4”})

Now the answer is “P1”.

=LOOKUP(2,

{#DIV/0!;2;3;3;4;#DIV/0!},

{“P1″;”P2″;”P3″;”P4”})

Answer is “P2”. Ok…let’s move on.

**The Brain**

**lookup_vector **is the brain of Robert’s solution:

1/FREQUENCY(0,1/(1+MMULT(SUMIFS(C$2:C$120,

A$2:A$120,”P”&ROW($1:$4),B$2:B$120,”V”&COLUMN(A:E)),

ROW($1:$5)^0)))

Inside SUMIFS he has used ROW and COLUMN to create the “P” and “V” values:

Select criteria1 “P”&ROW($1:$4) and press F9 key to see: {“P1″;”P2″;”P3″;”P4”}

Select criteria2 “V”&COLUMN(A:E) and press F9 key to see: {“V1″,”V2″,”V3″,”V4″,”V5”}

SUMIFS looks like this:

SUMIFS(C$2:C$120,

A$2:A$120,{“P1″;”P2″;”P3″;”P4”},B$2:B$120,{“V1″,”V2″,”V3″,”V4″,”V5”})

Let’s audit MMULT function using the F9 key to see the results:

Using F9 on MMULT’s array1 and array2 gives us:

=LOOKUP(2,

1/FREQUENCY(0,1/(1+MMULT({122000,0,128000,123000,175000;128000,0,171000,209000,123000;0,0,0,0,0;196000,0,111000,211000,229000},{1;1;1;1;1}))),

“P”&ROW($1:$4))

Examine the numbers inside MMULT. They are the same numbers found in range F3:J6 !MAGIC!

…the final result of MMULT is:

=LOOKUP(2,

1/FREQUENCY(0,1/(1+{548000;631000;0;747000})),

“P”&ROW($1:$4))

Notice something? {548000;631000;0;747000} are total column numbers from range K3:K6 !MAGIC!

and now evaluate what FREQUENCY function does:

=LOOKUP(2,

1/FREQUENCY(0,1/{548001;631001;1;747001}),

“P”&ROW($1:$4))

…eventually becomes this:

=LOOKUP(2,

1/{0;0;0;1;0},

“P”&ROW($1:$4))

and then:

=LOOKUP(2,

{#DIV/0!;#DIV/0!;#DIV/0!;1;#DIV/0!},

“P”&ROW($1:$4))

As Robert says in his comment below, FREQUENCY function returns the maximum of the row totals.

F9 key is your best friend. Use it until you see how Robert gets the same result but in only 1 formula. I just realized that it’s 10pm and I forgot to eat supper. Who needs food when you have an interesting formula to audit

A special thanks to Robert Gascon for this interesting Excel challenge. We need these kinds of distractions these days.

As amazing as Robert’s formula solution is there’s no way I’m going to attempt to create a shorter solution! Is it possible? Maybe…but I’ll let others try.

Follow Robert’s contributions to Microsoft Tech Community: https://techcommunity.microsoft.com/t5/user/viewprofilepage/user-id/280482

The post Robert Gascon’s Shortest Formula Challenge first appeared on My Spreadsheet Lab.]]>

**Task**

For each ‘Code’ concatenate ‘Batch 1’ numbers. Sort ‘Code’ and sort ‘Batch 1’ values for each ‘Code’.

Sounds easy but what approach should we use:

- fully automated (possibly complex solution) ?
- mostly automated (minor manual step needed but simpler solution) ?

Download my Excel file to see solution details (includes Robert Gascon’s ‘Old but Wise’ solution!).

**Pivot & Formula**

My favorite. If you can live with one manual step (sorting ‘Batch 1’ values) then this is it!

**Power Query**

My other favorite. I had to learn a trick in Power Query (modifying the M code: link).

**Dynamic Array Formula**

It was good practice but it did get complicated combining dynamic array functions.

**Old School Formula**

This just got ugly. I’m sure there’s a non dynamic array formula solution but mine was ugly

**Old But Wise**

Robert Gascon sent me his amazing solution. Notice how he used the INDEX function and also AGGREGATE function. Thanks Robert.

**Ultra Modern**

Daniel Choi sent me various dynamic array solutions and also a power query solution. Thank you Daniel. I will update the file later.

**Which solution do you prefer?**

**Do you have a better solution?**

This is me Kevin Lehrbass. I’m a Data Analyst and live in Markham Ontario Canada.

If you haven’t noticed I really like Microsoft Excel. It keeps evolving with new features so I’ll never learn everything. I guess that’s good. I’ll always have something to do.

The post Aggregating Text first appeared on My Spreadsheet Lab.]]>

To answer a question do we leave the data as is and use complex formulas or rearrange it so we can use easier formulas?

Below we see the awkward data we’ve been given.

My last **post** used formulas to answer a question directly on this data. I also rearranged it using formulas.

* Daniel Choi* sent me his amazing power query solution (

**Open Power Query**

Daniel created a named range called **RawData.** He loaded this range into power query.

*How do we get there?* On the ribbon click Data / Queries & Connections

Hover over RawData and click edit. This should open power query.

**Query Settings**

Below we see Daniel’s 11 transformation steps. Looks like he created most (all?) steps using menu choices. Each step creates M code which I’ve included in blue.

**Source**

= Excel.CurrentWorkbook(){[Name=”RawData”]}[Content]

He loaded named range RawData from the current workbook. It should be easy to swap RawData with another named range if necessary at a later date.

**Transposed Table**

#”Transposed Table” = Table.Transpose(Source)

Rows become columns. Click between steps **Source** and **Transposed Table** until you see how it works.

**Filled Down**

= Table.FillDown(#”Transposed Table”,{“Column1”})

In Column1 we see the month names are filled down.

**Table.Split**

= Table.Split(#”Filled Down”,3)

*******This is an amazing step! I had never seen Table.split before!*******

It splits the dataset into 3 tables (3 rows each). *Is there a way to make the 3 a variable based on the number of months? Could we use a power query function?*

**Converted to Table**

= Table.FromList(TableSplit, Splitter.SplitByNothing(), null, null, ExtraValues.Error)

List above with 3 tables is converted into a table (1 column, each row is a table) below.

**Transpose**

= Table.AddColumn(#”Converted to Table”, “Custom”, (OT)=> Table.AddColumn( Table.Transpose(Table.RemoveColumns(OT[Column1],{“Column1″})),”Month”, (IT)=> OT[Column1][Column1]{0}) )

I don’t understand what this step is doing. Hopefully over time I’ll figure it out.

**Removed Columns**

= Table.RemoveColumns(Transpose,{“Column1”})

This step is clear. Daniel created the Custom column. Column 1 is no longer needed.

**Expanded Custom**

= Table.ExpandTableColumn(#”Removed Columns”, “Custom”, {“Column1”, “Column2”, “Column3”, “Month”}, {“Column1”, “Column2”, “Column3”, “Month”})

If you’ve done a merge in power query you’ll be familiar with this. Click the double arrows, seen in pic above, to split out into columns. Now you see this:

Everything (Category, Letter, Value, Month) has it’s own column. This is the layout you need so you can:

- use simple formulas
- make a pivot
- upload data into a database

**Renamed Columns**

= Table.RenameColumns(#”Expanded Custom”,{{“Column1”, “Category”}, {“Column2”, “Letter”}, {“Column3”, “Value”}})

Each set (eg {“Column1”, “Category”}) contains the current column name followed by the new name.

**Reordered Columns**

= Table.ReorderColumns(#”Renamed Columns”,{“Month”, “Category”, “Letter”, “Value”})

Simple step of reordering the columns.

**Changed Type**

= Table.TransformColumnTypes(#”Reordered Columns”,{{“Month”, type text}, {“Category”, type text}, {“Letter”, type text}, {“Value”, type number}})

The last step changes the datatypes of the columns (or maybe it simply confirms the datatypes).

**Export Back to Sheet**

In the top left corner you see the **Close & Load** button. After the final transformation step above (Changed Type) he sent the rearranged data back to sheet Power Query.

With the rearranged data sent back to a sheet we’re done but let’s look at the complete M code back in power query. On the ribbon click Home / Advanced Editor. Here it is:

Source = Excel.CurrentWorkbook(){[Name=”RawData”]}[Content],

#”Transposed Table” = Table.Transpose(Source),

#”Filled Down” = Table.FillDown(#”Transposed Table”,{“Column1”}),

TableSplit = Table.Split(#”Filled Down”,3),

#”Converted to Table” = Table.FromList(TableSplit, Splitter.SplitByNothing(), null, null, ExtraValues.Error),

Transpose = Table.AddColumn(#”Converted to Table”, “Custom”, (OT)=> Table.AddColumn( Table.Transpose(Table.RemoveColumns(OT[Column1],{“Column1″})),”Month”, (IT)=> OT[Column1][Column1]{0}) ),

#”Removed Columns” = Table.RemoveColumns(Transpose,{“Column1”}),

#”Expanded Custom” = Table.ExpandTableColumn(#”Removed Columns”, “Custom”, {“Column1”, “Column2”, “Column3”, “Month”}, {“Column1”, “Column2”, “Column3”, “Month”}),

#”Renamed Columns” = Table.RenameColumns(#”Expanded Custom”,{{“Column1”, “Category”}, {“Column2”, “Letter”}, {“Column3”, “Value”}}),

#”Reordered Columns” = Table.ReorderColumns(#”Renamed Columns”,{“Month”, “Category”, “Letter”, “Value”}),

#”Changed Type” = Table.TransformColumnTypes(#”Reordered Columns”,{{“Month”, type text}, {“Category”, type text}, {“Letter”, type text}, {“Value”, type number}})

in

#”Changed Type”

Notice that each step references the previous step. Power Query gurus can modify this menu generated code or even write it directly without using the menus! I’ve tinkered around with it a couple of times. I’ve still got so much to learn.

Thanks again Daniel for sending me your solutions! Daniel shared some of his favorite power query links:

**Key Concept: Split Function:**

https://www.thebiccountant.

https://datachant.com/ –> Gil Raviv

https://blog.crossjoin.co.uk/ –> Chris Webb

https://www.excelguru.ca/ –> Ken Puls

**Bonus Solution**

Daniel just sent me a shorter 7 step solution! Oddly, I can’t upload his zip file but here is his M code:

Source = Excel.CurrentWorkbook(){[Name=”RawData”]}[Content],

#”Transposed Table” = Table.Transpose(Source),

#”Filled Down” = Table.FillDown(#”Transposed Table”,{“Column1”}),

#”Grouped Rows” = Table.Group(#”Filled Down”, {“Column1”}, {{“All”, each Table.Transpose(Table.RemoveColumns(_,{“Column1”})), type table}}),

#”Expanded All” = Table.ExpandTableColumn(#”Grouped Rows”, “All”, {“Column1”, “Column2”, “Column3”}, {“Column1.1”, “Column2”, “Column3”}),

#”Renamed Columns” = Table.RenameColumns(#”Expanded All”,{{“Column1”, “Month”}, {“Column1.1”, “Category”}, {“Column2”, “Letter”}, {“Column3”, “Value”}}),

#”Changed Type” = Table.TransformColumnTypes(#”Renamed Columns”,{{“Month”, type text}, {“Category”, type text}, {“Letter”, type text}, {“Value”, type number}})

in

#”Changed Type”

My name is Kevin Lehrbass. I’m a Data Analyst. I live in Markham Ontario Canada (outside of Toronto).

During this pandemic I’m trying to keep my mind busy. I guess it’s good that I’ve still got so much to learn about Excel and Power BI. Both include Power Query so that’s a bonus.

The post awkward-data/power-query-solution first appeared on My Spreadsheet Lab.]]>

(get my **Excel file**)

A nice normalized dataset would have 4 columns: Category, Month, Letter, Value. But we have this:

Each month has a group of 3 columns. If we had more months this pattern would repeat.

Add values in columns C, F, I for each month/letter combination. The answers look like this:

The key to solving this is finding the pattern in the awkward data layout.

Here are **3 formula solutions** that work on the data as is (no rearranging).

**Solution 1**

Long formula but I love OFFSET even if it’s a bit volatile.

**Solution 2**

An array (requires control shift enter).

**Solution 3**

This is probably my favorite solution. Short and it’s a normal formula (non array).

*Which formula is your favorite?*

*Do you have a better formula solution? (maybe using dynamic arrays?!)*

Rearrange the data so we can create a pivot. It should look like this:

**Power Query**

Can you rearrange this data using **Power Query**?

Your queries would need to adjust if the dataset grows (more months). I’d love to see if it’s possible!

**Formulas**

Well, that’s what we had to do in the old days so you should suffer too!

My file has my solution. It’s backwards compatible to any Excel version (no dynamic arrays).

It’s good practice to normalize using formulas. Note: formulas are in the same sheet as the data. Not a good idea but it’s a demo and it’s easier to read like this. Now we can create a simple pivot table with this data

**VBA**

Of course VBA could rearrange this data. But I’m too lazy to create all those loops right now!

My name is Kevin Lehrbass. I’m a data analyst from Canada.

I saw this awkward dataset just before covid crisis. Things have really changed…but there’s always been awkward data!

I play around with data to keep my mind busy and share ideas with others. I’m hoping that someone can solve this using Power Query. I gave up and went back to formulas.

The post Awkward Data first appeared on My Spreadsheet Lab.]]>

It makes key words standout. Helpful when scanning text. Letters qui are highlighted below.

Follow along with my Excel file (best to save it and then open it). It does contain macros (vba).

The code takes several inputs. Below you see the main 2 inputs. The grey button runs the code.

What is [Color 7]?

I was too lazy to create a list of color names. Column L contains a list you can reference.

The code behind this comes from:

http://dmcritchie.mvps.org/excel/colors.htm#colorindex

My code also includes an option to use a random letter and color. Just for fun!

It searches for all occurences of the search value in a cell. Then it goes to the next cell down and repeats this search. Text length X number of cells determines the time to run.

It’s **dangerous to run this code on thousands of rows as it could take a long time to complete**. Setting Application.ScreenUpdating = FALSE makes it run faster (we don’t see any results until it’s all finished) but it can still be super slow on a large dataset.

A conditional formatting rule (based on a formula) can highlight the entire cell if the search value exists inside it. True, the entire cell would have the color (not just exact text search value) but it’s easier & quicker than a vba solution.

=AND(‘vba Parameters’!$E$32=TRUE,ISNUMBER(SEARCH(‘vba Parameters’!$E$22,A2)))

The AND function contains two parts. Both must evaluate to TRUE in order to trigger the rule.

‘vba Parameters’!$E$32=TRUE

above is a switch to allow you to turn off the rule (even if the 2nd part below is true)

ISNUMBER(SEARCH(‘vba Parameters’!$E$22,A2))

above is the core functionality that looks for the search word inside the current row’s cell (A2).

My name is Kevin Lehrbass. I’m a Data Analyst. I live in Markham Ontario Canada.

A good way to keep my mind busy during these difficult times is to play around in Excel and also in Power BI. Exercising is also good and I’m doing that as well (but it’s not as fun).

The post Add color to letters inside a cell first appeared on My Spreadsheet Lab.]]>

I had to wear my winter coat and toque! The temperature was 2C (36F) but it was also windy. The wind chill “feels like” temperature was -4C (25F). Not really cold for me but it’s almost May! It was up to 12C (54 F) about 10 days ago.

Markham Ontario is in the southern part of Canada. Today the “feels like” wind chill figure was colder than Ottawa, Winnipeg, Quebec City, Edmonton and even Yellowknife!

Wind Chill Temperature = 35.74 + 0.6215×T – 35.75×V^{0.16} + 0.4275×T×V^{0.16}

This formula is from calculator.net It’s the standard wind chill formula for the United States & Canada.

*How does it work?*

T is the air temperature. V is the wind speed in mph. Wind chill “feels like” formula in column F is:

=35.74 + 0.6215*D2 – 35.75*E2^0.16 + 0.4275*D2*E2^0.16

Cell D2 is the temperature in Fahrenheit. Cell E2 is the wind mph also in Fahrenheit. In columns G and H I converted the values to Celsius using Excel’s handy CONVERT function.

Download my Excel file to see the formula in action.

I used theweathernetwork to get the temperature, wind mph, and feels like temperature. I also used the calculator and a double look chart from calculator.net (I recreated this in my Excel file).

Measuring the wind isn’t an exact science. The formula varies per region. From calculator.net:

The perception of lower temperatures caused by wind led to the development of many different formulas that attempt to qualitatively predict the effect of wind on this perceived temperature. Because wind chill temperature is not an exact science, weather services in different countries use standards relevant to their particular region and thus its estimates may differ from those provided by local weather services in other regions.

There’s also an interesting pdf here from journals.ametsoc.org

We’ve had various snow flurries over the past week but most of it melts before it hits the ground. It’s winter’s last stand (at least in the extreme south of Canada).

About 10 days ago it was up to 11 C (52 F). Oh well…it’s back and forth but it will get warmer soon

In Excel I’ve created a dynamic hyperlink to TheWeatherNetwork. Enter your State or Province and then your City. Click the link to see your current weather. It’s probably easier to use google but dynamic hyperlinks are fun! Note that it may not work if you have a state/province or city with spaces. Works great for single words:

https://www.theweathernetwork.com/ca/last-24-hours/ontario/markham

https://www.theweathernetwork.com/us/weather/texas/houston

Here’s an amazing Excel hyperlink resource from contextures.com (Deb lives on the other side on Toronto!).

It’s relative. If you are from a warm climate you’ll think that 10C (50F) is horribly cold. After a long winter that’s a nice day for me but it would be horribly cold in July (my summer) when it should be between 18C and 32C most days. If you are from the north part of Canada you’ll think that I’m a wimp as the winter’s are much colder and last longer!

Last year in July it was so hot & humid one day that I wrote a post about the Heat Index / Humidex.

My name is Kevin Lehrbass. I live in Markham Ontario Canada.

Growing up I was more comfortable using Fahrenheit rather than Celsius. Now after living in the Toronto area for 20 years I’ve converted over to Celsius for cold temperatures but for some strange reason it’s still a bit easier to think in Fahrenheit for warm temperatures.

The post Calculate Wind Chill in Excel first appeared on My Spreadsheet Lab.]]>(post inspired by Eugenia Chen’s wsj article)

“A sequence in which the numbers increase by the same amount at each step” (source).

Try to calculate this in your head:

Start with 2. Add 4 to it 10 times.

What’s the SUM? What’s the MEDIAN?

These are the values: 2, 6, 10, 14, 18, 22, 26, 30, 34, 38, 42. SUM is 242 and MEDIAN is 22.

Long sequences were very difficult even for mathematicians before calculators and computers.

It’s easy to create sequences in Excel. (Excel file: save to computer and then open).

These are the inputs.

Below is a non dynamic array solution that is backwards compatible.

And finally the calculations:

My solution above contains several steps. Let’s create sequence values from a single formula!

**Option A**

=SEQUENCE(C2,,A2,B2)

=TEXTJOIN(“, “,TRUE,SEQUENCE(C2,,A2,B2))

SEQUENCE is a dynamic array. Requires Office 365 Excel version. TEXTJOIN keeps it from spilling.

**Option B**

=TEXTJOIN(“, “,TRUE,B2*ROW(INDIRECT(“1:”&C2))-A2)

Formula above is an array and requires Control Shift Enter (just not Enter).

**Option C**

=B2*ROW(INDIRECT(“1:”&C2))-A2

Formula above doesn’t require Control Shift Enter (just Enter). It spills down into the cells below.

(download my pbix file.)

GENERATESERIES quickly creates a sequence:

Sequencev1 = GENERATESERIES(2,42,4)

Start with a 2, end at 42, intervals of 4. GENERATESERIES creates a table with 1 column.

Below I use variables but it does the same thing as above. So, is it really necessary?

Sequencev2 =

var SeqCount = 11

var SeqInterval = 4

var SeqStart = 2

var Final = GENERATESERIES(SeqStart,SeqCount*SeqInterval-SeqStart,SeqInterval)

return

Final

Both DAX versions above contain only internal inputs. Could I connect to an input from outside?

After experimenting I created a measure that I referenced when creating a new table.

Here’s the measure:

DistinctProductKeys = DISTINCTCOUNT(FactTable[Product Key])

Here’s the new table that references the measure above:

DynamicSequenceTBL = GENERATESERIES(1,[DistinctProductKeys],1)

If the distinct count changes this viz also updates. GENERATESERIES table function wants 3 single values (start, end, increment). It’s ok for a measure to be the end value. We could use measures for start and increment values as well.

My dad was really good at math. We would quiz him with multiplication and division questions. He seemed to be able to calculate anything. Hmm…maybe he would also fake some answers knowing that we wouldn’t know the difference.

I don’t think even he could have calculated a simple sum for a long sequence in his head. I bet he would’ve been a big spreadsheet and database fan. I remember that in the mid 1970s my older cousin had a calculator and it was a big deal. Around 1980 I went with my dad to visit his lawyer. He had one of those classic server room computers. I found it fascinating but it probably was less powerful than a smartphone from today.

**What’s next?** Imagine what things will be like forty years from now. Today’s technology will be ancient.

My name is Kevin Lehrbass. I live in Markham (near Toronto) Ontario Canada.

I’ve been working with data for almost 20 years now. SQL (various databases), Excel and now Power BI (DAX).

I find it all so interesting that I also have this blog

The post arithmetic progression first appeared on My Spreadsheet Lab.]]>(Post concept from Tom Urtis. Visit Tom’s blog)

In rare cases this could be extremely helpful. Instead of using vba to store all values and then use MIN and MAX functions you could use Tom’s vba solution.

(download my Excel file)

I modified Tom’s code a bit to also include an all time maximum (along with all time minimum).

Private Sub Worksheet_Change(ByVal Target As Range)

With Target

If .Address <> “$A$2” Then Exit Sub

If Len(.Value) = 0 Then

Exit Sub

End If

If .Value < Range(“B2”).Value Then Range(“B2”).Value = .Value

If .Value > Range(“C2”).Value Then Range(“C2”).Value = .Value

End With

End Sub

With Target

If .Address <> “$A$2” Then Exit Sub

If Len(.Value) = 0 Then

Exit Sub

End If

If .Value < Range(“B2”).Value Then Range(“B2”).Value = .Value

If .Value > Range(“C2”).Value Then Range(“C2”).Value = .Value

End With

End Sub

Worksheet_Change is a special kind of vba. It monitors a cell or range and performs an action when a condition(s) is met.

The code above performs various checks. If the activecell isn’t cell A2 then stop (exit sub). If the length of cell A2 is zero then stop. If the code is still going it means that the current cell is A2 and the length of cell A2 is not zero.

If .Value < Range(“B2”).Value Then Range(“B2”).Value = .Value

If cell A2 value is less than cell B2 value then take A2 value and put it in cell B2. This keeps the all time minimum value. The all time max works the same.

.

Years ago I had a discussion with a work colleague. He said “Oh, Excel can’t do that!”. I said “Oh yes it can”. I built this to prove him wrong

In range A1:D5 enter words. Those words will be stored in column F. If you type over a word the original word is still stored in column F (in the same order) and the new word is added further down.

.

.

Private Sub Worksheet_Change(ByVal Target As Range)

Dim nextrow As Integer, Savetxt As Range

Set Savetext = Range(“A1:D5”)

nextrow = Application.WorksheetFunction.CountA(Savetext)

With Target

If Target.Column > 4 Then Exit Sub

If Target.Row > 4 Then Exit Sub

Dim nextrow As Integer, Savetxt As Range

Set Savetext = Range(“A1:D5”)

nextrow = Application.WorksheetFunction.CountA(Savetext)

With Target

If Target.Column > 4 Then Exit Sub

If Target.Row > 4 Then Exit Sub

On Error GoTo Handler

If Len(.Value) = 0 Then

Exit Sub

End If

Exit Sub

End If

Range(“F” & nextrow + 1).Value = .Value

End With

Handler:

Handler:

End Sub

I got a bit lazy with the code and used On Error GoTo Handler to hide my incompetence (you’ll see).

Well…thanks again to Tom Urtis. Unfortunately I couldn’t find my original code for the multi cell input area but I was able to recreate it to do pretty much the same thing. I think now I’ll get some more coffee and study Power BI and DAX.

**About Tom Urtis**

He’s an Excel consultant, Excel MVP, and Excel legend! Tom is also a big sports fan (baseball and football). Visit his blog: https://www.atlaspm.com/

**About Me**

My name is Kevin Lehrbass. I’m a Data Analyst from Markham Ontario Canada.

In these difficult times of Covid-19 I’m keeping my mind occupied here at home by playing around in Excel and Power BI. Wait…I always do that! I’ve also calculated the optimal balance of news and comedy. Oh yeah…these are my dogs Fenton and Cali. Starting yesterday the neighbourhood kids aren’t running around outside so they have nothing to bark at.

The post Worksheet_Change Keep All Time Min Max Numbers first appeared on My Spreadsheet Lab.]]>

Washing hands, staying home, etc slows down the spread which flattens the curve, relieves pressure on hospitals and saves lives. Let’s all do our part.

We’ve seen the chart on the news. Here’s one from CBC news:

The health care system is limited (dotted constant line). We need to reduce the height of the red curve so it fits within health care system capacity.

Download my Excel file (save to computer and then open it).

My formulas create **fake demo data** for 2 bell curves. The red one quickly spikes upward and past the health care dotted line while the green one (based on our positive actions) doesn’t peak as high and spreads out more to the right.

**NORM.INV function**

NORM.INV function creates the bell curves. It has 3 arguments:

‘mean’ and ‘standard_dev’ define the curve’s dimensions. ‘mean’ is the average of all the numbers and ‘standard_dev’ defines the spread of the data-set. A big number = a big spread (flatter), a small number = a peak around the mean.

‘probability’ creates a value within this dataset.

**NORM.INV Formula**

=NORM.INV(RAND(),$B$2,$B$3) depends on variables in cells B2 and B3. RAND function creates a random value within our data-set. If we drag this formula down far enough we’ll have a smooth curve clustered around the mean.

**Binning the Numbers**

I created groups (aka binning) to make it easier to read the data.

Finally I created the chart that we saw above. Don’t forget that the data changes due to the volatile RAND function…and feel free to play around with the ‘mean’ and ‘standard_dev’ variables.

Download my pbix file.

To make it interesting I decided to **create everything in Power BI. No external data allowed!**

I researched the DAX to create bell curve numbers. But it’s not possible to create a measure until we have a table!

**How can we create a table without importing data?** I used table function GENERATESERIES.

On the ribbon click ‘Modeling’ and ‘New Table’ then enter this DAX and press enter:

Table1 = GENERATESERIES(1,2000,1)

Click the ‘Data’ icon on the left side of the screen to see our new table! It has one field called ‘Value’ with 2000 rows.

**Add DAX helper columns to create bell curve data.**

As there’s only 1 table new columns are automatically added to Table1. Click ‘New Column’ and add DAX below:

BellCurve1 = ROUNDDOWN(Norm.inv(rand(),300,60),0)

BellCurve2 = ROUNDDOWN(Norm.inv(rand(),300,100),0)

Now Table1 has 3 columns: Value, BellCurve1, BellCurve2.

**Making the Chart**

Hover over field BellCurve1. On right side click the 3 dots and select ‘New Group’. Change bin size to 50, press ‘Ok’.

Drag new column ‘BellCurve1 (bins)’ into the canvas. It should default to a Clustered Column Chart.

Add ‘BellCurve1’ to the Value area. With the chart selected, select ‘Line Chart’. Add ‘BellCurve2’ to the Value area.

Add constant line: Under Visualizations click magnifying glass (Analytics), ‘Constant line’, ‘Add’, with value of 200000.

Here’s the Power BI demo chart. Remember that it’s based on RND (random) so it changes.

**Make Table Alternative #1**

GENERATESERIES is amazing but it’s not the only way. I created the same table (1 counter column) in Power Query!

- Open Power Query: On the ribbon select ‘Home’ and ‘Transform data’ (could be ‘edit queries’ in your version)
- Home / New Source / Blank Query
- In formula bar paste this code: =List.Numbers(1,2000,1)
- Top left click ‘To Table’ and press ‘ok’
- M code looks like this: = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error)
- Top left click ‘Close & Apply’

Our ‘Query1’, created inside Power Query, is now a table here in Power BI’s data model. Cool!

**Make Table Alternative #2**

This DAX method is longer than GENERATESERIES but it’s interesting and worth reviewing.

Table2 =

VAR MINcounter = 1

VAR MAXcounter = 2000

RETURN

SELECTCOLUMNS(

CALENDAR( MINcounter, MAXcounter ),

“xNumber”,

INT ( [Date] )

)

I could use CALENDAR function to create a new table. On ribbon: Home / New Table:

zTable = CALENDAR(1,2000)

Yes, it creates a new table but it has 2000 dates and we want numbers 1 to 2000. I tried to wrap CALENDAR function with INT function but that didn’t work (probably because CALENDAR is a table function not a column).

So, wrap CALENDAR function with SELECTCOLUMNS to convert dates to integers. SELECTCOLUMNS has 3 parts:

- a table: CALENDAR( MINcounter, MAXcounter )
- a column name: “xNumber”
- an expression: INT([Date])

The DAX code in Table2 creates a table with 1 column named ‘Date’ and 2000 rows.

Note: I used variables (VAR) to store min and max numbers later used inside CALENDAR function. Not a big difference but in longer DAX statements using variables makes it so much easier to read.

Creating the excel file, pbix file and writing this post really helped me focus on something else besides you know what. I don’t know everything about Excel but I’ve used it for many years. The real challenge was researching the M and DAX code and implementing it in Power BI. I have a fair amount of theoretical knowledge but hands on building is essential.

More positive distractions include watching:

- Big Bang Theory
- Two and a Half Men
- Seinfeld
- Men in Black 3

We need to be informed but we can’t worry constantly. **What are you doing to have fun or distract yourself?**

My name is Kevin Lehrbass. I’m a Data Analyst and live in Markham Ontario.

Hopefully we’ll look back at the Covid-19 crises saying things like “I’m glad we took those steps to slow it down”. Time will tell.

Here’s a post about standard deviation based on height. It proves that I’m not always a freak. It depends on my location.

The post Flatten the Curve first appeared on My Spreadsheet Lab.]]>