What DAX will calculate amount for same month in last months report? - powerbi

I am struggling to work out the correct DAX measure to sum amount for the same period (e.g. month) but from a previous report.
Full Year forecast reports are provided monthly with a Report Date of EOMONTH for the month provided. These contain Amounts for each month in the year (called Period End Date, also dated EOMONTH). These are imported to a "Forecasts" table.
The following sample table replicates data from 3 separate monthly forecast reports, but rows are limited only to Jan-Mar months for simplicity (note - I have created Amounts based on concatenated month numbers for Report Date & Period End Date, for ease of identifying correct amounts in expected results):
Report Date
Period End Date
Amount
31 Jan 22
31 Jan 22
11
31 Jan 22
28 Feb 22
12
31 Jan 22
31 Mar 22
13
28 Feb 22
31 Jan 22
21
28 Feb 22
28 Feb 22
22
28 Feb 22
31 Mar 22
23
31 Mar 22
31 Jan 22
31
31 Mar 22
28 Feb 22
32
31 Mar 22
31 Mar 22
33
There is a "Dates" table, relationship = Dates[Date] to Forecasts[Period End Date], 1 to many, single direction. The user selects a single Report Date via a slicer creating filter context, visuals categories use Forecasts[Period End Date] for row context.
I've tried various methods but so far am unsuccessful, I'm having a mind meld and am probably overthinking this and therefore not looking at best method. One DAX example is:
Prev Month Forecast =
VAR _SelRptDate = SELECTEDVALUE( 'Forecasts'[Report Date] )
VAR _PreRptDate = EOMONTH( _SelRptDate, -1)
VAR _Result =
CALCULATE(
SUM( 'Forecasts'[Amount] ),
FILTER( 'Forecasts', 'Forecasts'[Report Date] = _PreRptDate)
)
RETURN _Result
Ideally I'd like to use an appropriate time intelligence function if one works for this. Happy to create inactive relationships if needed (e.g. Dates[Date] to Forecasts[Period End Date]). Preferably a Measure, not calculated column.
The following examples demonstrate expected results based on Report Date selected by the user:
Report Date = 28 Feb 22
Period End Date
Amount
Amount in Previous Report Date for Same Period End Date
31 Jan 22
21
11
28 Feb 22
22
12
31 Mar 22
23
13
Report Date = 31 Mar 22
Period End Date
Amount
Amount in Previous Report Date for Same Period End Date
31 Jan 22
31
21
28 Feb 22
32
22
31 Mar 22
33
23
Really appreciate any help / direction.
Thanks

Related

Filter only MAX month figures Power BI Desktop

I have a table of values for orders per month by region that looks like this:
Orders Table
Orders (YTD)
Month
Year
1
Jan
2021
4
Feb
2021
4
Mar
2021
5
Apr
2021
14
May
2021
16
Jun
2021
17
Jul
2021
19
Aug
2021
22
Sep
2021
24
Oct
2021
34
Nov
2021
35
Dec
2021
1
Jan
2022
3
Feb
2022
4
Mar
2022
Along with a table that orders the months in sequence as below, that will be modelled to order the months in the first table so that they appear in sequence in graphs.
Monthly Sequence Table
Month Sequence
Month
1
Jan
2
Feb
3
Mar
4
Apr
5
May
6
Jun
7
Jul
8
Aug
9
Sep
10
Oct
11
Nov
12
Dec
Upon closer inspection of my data, I have realised that the number of orders per month are not the raw figure per month, but a cumulative total for every order in the calendar year so far (new orders for month + orders for preceding month). Firstly, I want to calculate the correct sum of orders per year, which should actually just be the MAX month from the orders table. Of course in most years this will be December, but for the current year it needs to be the latest month. I wanted to use a measure to calculate the MAX 'Monthly Sequence Table'[Month Sequence] number from each table, by year. I thought maybe a filter function would be used but could not work out exactly how to do this in DAX.
Secondly, and similarly, I want to calculate the actual number of orders per each individual month using DAX. In this case, I want to take the Orders (YTD) total for that month/year combination and subtract it from its immediately preceding month. What would the formula look like for this?
Thanks in advance.

Power BI DAX - using monthly totals instead of grand totals

I'm having issues with the following calculation:
"Kunden Anzahl" refers to the number of customers via distinct count, with %SG being the percentage. The issue is that Power BI is taking the grand total of 21.801 for the calculation of the percentages instead of the corresponding total of each month (19.337 for 12/2020, 21.391 for 12/2021). This results in the total percentage being less than 100% except for 01/2022.
So what it should do is divide each customer count by the total of its corresponding month - for example 6.326 / 19.337 = 32.7%
I've tried grouping the denominator by month and ignoring the categories by using REMOVEFILTERS but this just replicates the first column.
Any help is greatly appreciated!
Reference to comment:
Data model looks like this - JJJJMM is connected to a date table:
Assuming your data looks like this
CustomerID
Category
Month
14
A
01 January 2021
21
A
01 January 2021
29
B
01 January 2021
39
B
01 January 2021
6
B
01 January 2021
18
C
01 January 2021
34
A
01 February 2021
29
A
01 February 2021
4
A
01 February 2021
17
B
01 February 2021
24
B
01 February 2021
39
B
01 February 2021
11
B
01 February 2021
1
B
01 February 2021
42
A
01 March 2021
46
A
01 March 2021
2
A
01 March 2021
30
B
01 March 2021
DAX Calculation
CountDistinct (%) =
VAR CurrentValue =
DISTINCTCOUNT ( 'Table'[CustomerID] )
VAR AllMonth =
CALCULATE (
DISTINCTCOUNT ( 'Table'[CustomerID] ),
ALL ( 'Table' ),
VALUES ( 'Table'[Month] )
)
RETURN
DIVIDE ( CurrentValue, AllMonth )
Output
Reference
https://www.sqlbi.com/articles/using-allexcept-versus-all-and-values/

How can I create a dynamic measure or column for an array in PowerBI?

Excel Example
I am attempting to recreate a similar chart in PowerBI as I did in excel seen below:
Here I have revenue per day. The chart shows the percent of days where revenue exceeds a fixed amount (100, 200, etc).
In PowerBI I know how to recreate the table that the chart is based on by defining a column, but it's not dynamic. I can't apply filters to change the column values.
I know I can apply filters to measures but when I try to replicate the formula as a measure I get an error, which I assume is due to the formula trying to return an array of values.
Here is my formula for the fixed column version:
table2 column = countx(
filter(
DayRevenueTable,
[Revenue]>Table2[DayRevenueExceeding])
,[Day])
/Total
Assuming your table looks like this:
Date
Revenue
04 January 2022
102
11 January 2022
162
17 January 2022
180
02 January 2022
185
12 January 2022
203
05 January 2022
278
01 January 2022
353
16 January 2022
449
14 January 2022
500
06 January 2022
515
08 January 2022
582
10 January 2022
600
03 January 2022
618
09 January 2022
626
13 January 2022
626
15 January 2022
706
18 January 2022
765
07 January 2022
895
You need to first create a table with your fixed values. Basically is the same concept as creating a parameter.
Using that table as a reference, you can create your calculation around Fixed Values[Value].
DAX: Fixed Values
Fixed Values = GENERATESERIES(100,1000,100)
DAX: Days When Revenue Exceeds Amount
Days When Revenue Exceeds Amount =
VAR CurrentFixedValue =
SELECTEDVALUE ( 'Fixed Values'[Value] )
VAR CountValues =
CALCULATE (
COUNTROWS ( 'Table' ),
'Table'[Revenue] < ( CurrentFixedValue + 100 )
)
VAR AllValues =
CALCULATE ( COUNTROWS ( 'Table' ), ALLSELECTED ( 'Table' ) )
VAR Calc =
DIVIDE ( CountValues, AllValues )
RETURN
Calc
Output

Is there a recommended Power BI DAX pattern for calculating monthly Days Sales Outstanding (a.k.a. Debtor Days) using the Countback method?

Is there a recommended Power BI DAX pattern for calculating monthly Days Sales Outstanding (a.k.a. DSO or Debtor Days) using the Countback method?
I have been searching for a while and although there are many asking about it, there is no working solution recommendation I can find. I think that is perhaps because nobody has set out the problem properly so I am going to try to explain as fully as possible.
DSO is a widely-used management accounting measure of the average number of days that it takes a business to collect payment for its credit sales. More background info on the metric here: https://www.investopedia.com/terms/d/dso.asp
There are various options for defining the calculation. I believe my requirement is known as the countback method. My data set is a fairly large star schema with a separate date dimension, but using the below simplified data set to generate a solution would totally point me in the right direction.
Input data set as follows:
Month No
Month
Days in Month
Debt Balance
Gross Income
1
Jan
31
1000
700
2
Feb
28
1100
500
3
Mar
31
900
400
4
Apr
30
950
600
5
May
31
1000
400
6
Jun
30
1100
550
7
Jul
31
900
700
8
Aug
31
950
500
9
Sep
30
1000
400
10
Oct
31
1100
600
11
Nov
30
900
400
12
Dec
31
950
550
The aim is to create a measure for debtor days equal to the number of days of average daily income per month we need to count back to match the debt balance.
Starting with Dec as an example in 3 steps:
Debt Balance= 950, income = 550. Dec has 31 days. So we take all
31 days of income and reduce the debt balance to 400 (i.e. 950 - 550) and go back to the previous month.
Remaining Dec Debt balance =
400. Nov Income = 700. We don't need all of the daily income from Nov to match the rest of the Dec debt balance. 400/700 x 30 days in
Nov = 17.14 days
We have finished counting back days. 31 + 17.14 = 48.14 debtor days
Nov has a higher balance so we need 1 more step:
Debt balance= 1500, income = 700. Nov has 30 days. So we take all 30 days of income and reduce the debt balance to 800 (i.e. 1500 - 700) and go back to the previous month.
Remaining Nov Debt balance = 800. Oct Income = 600. Oct has 31 days. So we take all 31 days of income from Oct and reduce the Nov debt balance to 200 (i.e. 1500 - 700 - 600)
Remaining Nov debt balance = 200. Sep Income = 400. We don't need all of the daily income from Sep to match the rest of the Nov debt balance. 200/400 x 30 days in Sep = 15 days
We have finished counting back days. 30 + 31 + 15 = 76 debtor days
Apr has a lower balance so can be resolved in one step:
Debt Balance = 400, income = 600. Apr has 30 days. We don't need all of Apr Income as income exceeds debt in this month. 400/600 * 30 = 20 debtor days
The required solution for Debtor days in the simplified data set is therefore shown in the right-most "Debtor Days" column as follows:
Month
Month
Days
Debt Balance
Gross Income
Debtor Days
1
Jan
31
1000
700
2
Feb
28
1100
500
54.57
3
Mar
31
900
400
59.00
4
Apr
30
400
600
20.00
5
May
31
600
400
41.00
6
Jun
30
800
550
49.38
7
Jul
31
900
700
41.91
8
Aug
31
950
500
50.93
9
Sep
30
1000
400
65.43
10
Oct
31
1100
600
67.20
11
Nov
30
1500
700
76.00
12
Dec
31
950
550
48.14
I hope the above explains the required calculation sufficiently. Of course it needs to be implemented as a measure rather than a calculated column as in the real world it needs to work with more complex scenarios with the user defining the filter context at runtime by filtering and slicing in Power BI.
If anyone can recommend a DAX calculation for Debtor Days, that would be great!
This works on a small example, probably this may not work on a large model.
There is no easy way to do that, DAX isnt a programing language and we canot use loop / recursive statements etc. We have many limitations;
We can only mimic this behavior by bulk/ force calculate (which is resource consuming task). The most interesting part is variable _zz where we calculate for each row 3 version of the main table limited to 1/2/3 rows (as you see we hardcode some value - i consider that we can find result in max 3 iteration). You can investigate this if you want by adding NewTable from this code:
filter(GENERATE(SELECTCOLUMNS(GENERATE(Sheet1, GENERATESERIES(1,3,1)),"MYK", [MonthYearKey], "MonthToCheck", [Value], "Debt", [Debt Balance]),
var _tmp = TOPN([MonthToCheck],FILTER(ALL(Sheet1), Sheet1[MonthYearKey] <= [MYK] ), Sheet1[MonthYearKey], DESC)
return row("IncomAgg", SUMX(_tmp, Sheet1[Gross Income]) )
), [IncomAgg] >= [Debt])
Next, I try to find in our Table Variable 2 information, how many months back we must go.
Full code (I use MonthYearKey for time navigating purpose):
Mes =
var __currRowDebt = SELECTEDVALUE(Sheet1[Debt Balance])
var _zz = TOPN(1,
filter(GENERATE(SELECTCOLUMNS(GENERATE(Sheet1, GENERATESERIES(1,3,1)),"MYK", [MonthYearKey], "MonthToCheck", [Value], "Debt", [Debt Balance]),
var _tmp = TOPN([MonthToCheck],FILTER(ALL(Sheet1), Sheet1[MonthYearKey] <= [MYK] ), Sheet1[MonthYearKey], DESC)
return row("IncomAgg", SUMX(_tmp, Sheet1[Gross Income]) )
), [IncomAgg] >= [Debt]), [MonthToCheck], ASC)
var __monthinscoop = sumx(_zz,[MonthToCheck]) - 2
var __backwardrunningIncom = sumx(_zz,[IncomAgg])
var _calc = CALCULATE( sum(Sheet1[Days]), filter(ALL(Sheet1), Sheet1[MonthYearKey] <= SELECTEDVALUE( Sheet1[MonthYearKey]) && Sheet1[MonthYearKey] >= SELECTEDVALUE( Sheet1[MonthYearKey]) - __monthinscoop ))
var __twik = SWITCH( TRUE()
, __monthinscoop < 0 , -1
, __monthinscoop = 0 , 1
, __monthinscoop = 1 , 3
,0)
var __GetRowValue = CALCULATE( SUM(Sheet1[Gross Income]), FILTER(ALL(Sheet1), Sheet1[MonthYearKey] = (SELECTEDVALUE( Sheet1[MonthYearKey]) + __monthinscoop - __twik)))
var __GetRowDays = CALCULATE( SUM(Sheet1[Days]), FILTER(ALL(Sheet1), Sheet1[MonthYearKey] = (SELECTEDVALUE( Sheet1[MonthYearKey]) + __monthinscoop - __twik)))
return
_calc+DIVIDE(__GetRowValue - (__backwardrunningIncom - __currRowDebt), __GetRowValue) * __GetRowDays

How to have a measure always show the ratio as percentage in scorecard/textbox POWER BI

There are many method of having a measure to show percentage in a column of table ,
but cannot find a method to always show the ratio of a SPECIFIC group in percentage between two category.
data sample:
YEAR MONTH TYPE AMOUNT
2020 Jan A 100
2020 Feb A 250
2020 Mar A 230
2020 Jan B 158
2020 Feb B 23
2020 Mar B 46
2019 Jan A 499
2019 Feb A 65
2019 Mar A 289
2019 Jan B 465
2019 Feb B 49
2019 Mar B 446
2018 Jan A 13
2018 Feb A 97
2018 Mar A 26
2018 Jan B 216
2018 Feb B 264
2018 Mar B 29
2018 Jan A 314
2018 Feb A 659
2018 Mar A 226
2018 Jan B 469
2018 Feb B 564
2018 Mar B 164
My Goal is always show the percentage of A compare with the total amount
YEAR and MONTH are used to synchronize with slicer.
e.g. I select YEAR = 2020 , MONTH = Jan
100/258 = 38%
Manually inputted in textbox
First, Create these following 3 measures in your table-
1.
amount_A =
CALCULATE(
SUM(pie_chart_data[AMOUNT]),
FILTER(
ALLSELECTED(pie_chart_data),
pie_chart_data[TYPE] = "A"
)
)
2.
amount_overall =
CALCULATE(
SUM(pie_chart_data[AMOUNT]),
ALLSELECTED(pie_chart_data)
)
3.
amount_A_percentage = [amount_A]/[amount_overall]
Now, add both measure amount_A and amount_overall to your donut chart's values column. And place the amount_A_percentage measure to a Card and place the card in center of the Donut chart. The presentation will be as below finally-