Power BI - Add column using M to count 2 years of data - excel

I have 2 years of data on a bar chart and an x-axis that I created by concatenating Year and Month Number into a separate column called "Year - Month"
Number.ToText([Year]) & Number.ToText([Month Number])
The result is
20171
20172
20173
20174
...
201711
201712
20181
20182
20183
20184
20185
...
How can I create a calculated column in M to add a number column that counts 1 - 24 which will allow me to sort the x axis on the chart?
Also how can I make this dynamic allowing any new data in the report to auto increment to 25, 26, 27... into year 3?

Try in Power Query editor:
Go to > Add Column > Index Column > From 1

Not exactly what you're asking for, but you might prefer to create a date column that you could use for your axis instead.
= #date([Year],[Month Number],1)

Related

fill in mulitple excel cells based on multiple cell values similar to a gantt chart

I would like to create a Gantt chart like with excel based on task start and duration cell values. I have an excel as below:
Item
Start
Duration(months)
Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
1
Q1
6
2
Q2
3
3
Q2
1
Start column indicates the quarter-calender the task starts.
Duration indicates the number of months the task can take to complete.
Based on these two value I would like to have a formula in columns Jan-Aug to fill with x appropriately
Desired output:
Item
Start
Duration(months)
Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
1
Q1
6
x
x
x
x
x
x
2
Q2
3
x
x
x
3
Q2
1
x
I dabbled with XLOOKUP and OFFSET with no luck. Tried creating defined names as in Microsoft template with no luck
You can try the following formula in cell D2:
=LET(months, MONTH(D1:O1), dur, C2:C4, Qs, B2:B4,
QsUx, SORT(UNiQUE(Qs)), start, XLOOKUP(Qs, QsUx, SEQUENCE(ROWS(QsUx),,1,3)),
end, start + dur - 1,
GEN_X, LAMBDA(idx, MAP(start, end, LAMBDA(ss, ee,
LET(month, INDEX(months,,idx), IF(MEDIAN(ss, ee, month) = month, "x",""))))),
DROP(REDUCE("", months, LAMBDA(acc, m, HSTACK(acc, GEN_X(m)))),,1)
)
and here is the output:
Note: The formula generates the entire grid, there is no need to expand the formula.
Explanation
It uses the following two ideas:
To populate the grid using the MEDIAN function. Check the answer provided by #JosWoolley: Sum unique day count within a date ranges.
DROP/REDUCE/HSTACK pattern to iterate over all the month's columns to generate the x's for a given column. Check the answer provided by #DavidLeal to the question: how to transform a table in Excel from vertical to horizontal but with different lengths.
The rest is just to prepare/transform the input data in the format we want. The LET function is used for easy reading and composition.
The row with the months: D1:O1 was generated in date format as follows:
=EDATE(DATE(2022,1,1), SEQUENCE(1,12,0))
Now the months variable will have the corresponding months via the MONTH function, it returns the following sequence of values: 1,2,..,12.
QsUx has the unique names of quarters sorted. Then we can calculate the start date as follow:
XLOOKUP(Qs, QsUx, SEQUENCE(ROWS(QsUx),,1,3))
SEQUENCE generates the following sequence: 1,4,7,..etc. (as many values as rows have QsUx). Representing the starting month of each quarter.
Note: This is just one approach. The start can be obtained, by extracting the number of the Quarter and building a sequence: 1, prev. value + 3, for example. Alternatively, since we have only four quarters just a simple constant array with all the Qs and their corresponding start dates. The approach used in the main formula is more generic because the Gantt can be expanded to more than one year and it still works.
Having the start date, then we can calculate the corresponding end date as follows: start + dur - 1.
The user LAMBDA function GEN_X generates the x's values for a given column of months indicated by the idx as input argument (representing the corresponding month).
Finally, we use the DROP/REDUCE/HSTACK pattern to append each column to complete the grid.
Note: The solution assumes no excel version constraints as per the tag listed in the question, so all existing excel functions are available. If you have some limitations for example you don't have the DROP function, let me know to try to find some alternative.

DAX Calculation with Datesbetween Ignores all Datefilters

I am starting with DAX.
I have a standard date table and a table with sales. When I try to show the running total with the below formula the month filter is ignored, as shown:
The formula is:
Prov YTD VJ 2:=CALCULATE(FIRMPROV[SumProv];
All(Kalender[Jahr]);
DATESBETWEEN(Kalender[Datum];
Min(START_YTD[start_ytd]);
MIN(END_YTD[end_ytd])))
A good way to calculate running totals is by using the FILTER and EARLIER functions, but a prerequisite to that is that you need to create a Month Number column (ie. Jan = 1, Feb = 2, Mar = 3, etc.)
Then, you can use the following DAX:
CALCULATE(SUM([SumProv]),ALL(FirmProv),FILTER(FirmProv,[Month Number]<=EARLIER([Month Number])))
EARLIER basically is DAX's way of interpreting the current row. You're telling DAX to sum ALL of the rows in the FirmProv table where the month number is less than or equal to the current row (AKA running total).
Here's the result (note the numbers aren't exactly like yours, I just quickly mocked it up to show the example):

How to create a ranged frequency table using Excel Cube functions

What I want is to create something like this (see image) in Excel that pulls in the frequency of my chosen category from a table within my PowerPivot data model. The output would also need to be dynamically filtered via a cell reference.
Using the below data set as an example:
Week 1 A
Week 1 A
Week 1 A
Week 1 A
Week 1 A
Week 1 B
Week 1 B
Week 1 C
Week 1 D
Week 2 A
Week 2 A
Week 2 A
Week 2 A
Week 2 A
Week 2 A
If I filtered to Week 1 through my set expression, the following output would be produced
1 2
2-4 1
5+ 1
However, if filtered to Week 2, this output would be produced
1 0
2-4 0
5+ 1
I say 'set expression' because I am assuming the way to do this is through the CUBESET function possibly with a MDX FILTER function and the CUBESETCOUNT functions though I am open to other suggestions. Even though the example doesn't show it, I need the expression to filter multiple dimensions from my cube, in particular, date, which is found in my calendar table.
I also understand that I could create specific measures within PowerPivot to handle this (sample formula below), but I would like to know if this is possible via the CUBESET function.
Freq (5+):=CALCULATE(
DISTINCTCOUNT([myColumn]),
FILTER(myTable,
COUNTROWS(FILTER('myTable', [myColumn] = EARLIER([myColumn]))) >=5 )
)
I am also open to creating a helper column within my Powerpivot table.
Thanks,

how to show 0 data point in visualization when data is missing in data table?

i have one visualization,
on x-axis i have months of date column,
on Y-axis i have unique count of issues.
i have 2 filters in text area,
when i am selecting some values in filter 1 or filter 2 than if count is not available for any month than i need 0 for that month.
Lets suppose i have 5 month(Jan to may) data in my data table, i selected 1 value from filter 1 and 1 value from filter 2, if that combination data is not available for march and April, than trend should show 0 count for march and April.
it is a trend line so if count is not available for any month for selected filters than it should show 0. any lead will help.
TIA
Go to the x-axis settings of your vizualization, click on settings next to your month variable and with the categorical options, choose show all values. If you have a bar chart it will show you the 0 values. In a line chart Spotfire will continue the line as if there are no missing values. In this case you need to add markers to your line.
1:

Count in Excel over a range using 2 columns of data

Tried looking for an answer first...
I want to count the number of items that were open between 1-7 days, 8-14 days, etc etc (open and closed on same day = open for 1 day)
Is there a way I can count this directly using the 2 columns holding the data without creating a 3rd column for Date Closed - Date Opened +1??
So in this example, what can I use to calculate that the number of 1-7 days items is 2? (Items 1 and 3)
Item # Open Closed
1 01/06/2014 01/06/2014
2 05/05/2014 20/06/2014
3 10/06/2014 16/06/2014
4 08/06/2014 15/06/2014
5 02/06/2014 19/06/2014
Many thanks :o)
One way would be to use the versatile SUMPRODUCT:
=SUMPRODUCT(1*(C2:C6-B2:B6+1>7))
assuming your start date column is B and end date column is C. The second part of the formula will determine all date spans which are larger than 7, and the 1* will convert the TRUE values to 1 in order to sum them up.

Resources