Need correct total, Power BI table - powerbi

I have a measure in which i am dividing total number of contractual months from total number of months. I am getting correct result, but the total in the bottom is not correct.
The first column is a unique ID, Third and fourth columns are numerator and denominator, Second column is the result of the division, I want to count those IDs, where the division is between 0.75 and 1.00
Here are my calculations
Var Check=DIVIDE([Month of Engagement],[Months In Contract L30])
RETURN
IF(HASONEVALUE('Fact - TABLE'[ID]),IF(Check>=0.75 && Check<=1.00,DISTINCTCOUNT(ID),0),SUMX('Fact - TABLE',IF(Check>=0.75 && Check<=1.00,DISTINCTCOUNT(ID),0)))
Please let me know, how to solve this.

For Total, you should change 3th parameter of IF to:
CALCULATE (
SUMX (
CALCULATETABLE (
tab,
FILTER (
ALL ( tab ),
VAR __Check =
DIVIDE ( 'Tab'[Month of Engagement], Tab[Months In Contract L30] )
RETURN
__Check < 1
&& __Check > 0.75
)
),
1
)
)
Below my test example (for testing only I have put check variable in first IF
IF ( Check >= 0.75 && Check <= 1.00, Check, 0 ) to see if check calculation is correct):
Measure =
VAR Check =
DIVIDE (
SUM ( 'Tab'[Month of Engagement] ),
SUM ( Tab[Months In Contract L30] )
)
RETURN
IF (
HASONEVALUE ( 'Tab'[ID] ),
IF ( Check >= 0.75 && Check <= 1.00, Check, 0 ),
CALCULATE (
SUMX (
CALCULATETABLE (
tab,
FILTER (
ALL ( tab ),
VAR __Check =
DIVIDE ( 'Tab'[Month of Engagement], Tab[Months In Contract L30] )
RETURN
__Check < 1
&& __Check > 0.75
)
),
1
)
)
)

Measure =
VAR Check =
DIVIDE (
[Month of Engagement] ,
[Months In Contract L30]
)
RETURN
IF (
HASONEVALUE ( 'Tab'[ID] ),
IF ( Check >= 0.75 && Check <= 1.00, Check, 0 ),
CALCULATE (
SUMX (
CALCULATETABLE (
'Tab',
FILTER (
ALL ( 'Tab' ),
VAR __Check =
DIVIDE ( [Month of Engagement], [Months In Contract L30] )
RETURN
__Check >=0.75
&& __Check <=1.00
)
),
1
)
)
)
Here are the brief definitions
Month of Engagement:= CALCULATE(COUNTROWS(
SUMMARIZE('table','table'[MONTH_OF_ENGAGEMENT],"Count",DISTINCT('table'[MONTH_OF_ENGAGEMENT]))),'table'[FREQUENCY_FLAG]="Y")+0
Here MONTH_OF_ENGAGEMENT is value extracted from a date as YYYYMM
Months In Contract L30:=
Var DBRefreshDate30= [Database Refresh Date]-30
RETURN
DATEDIFF(DBRefreshDate30,[Database Refresh Date],MONTH)+1
Now basis on dividing Month of Engagement from Months In Contract L30, i am deriving a percentage, and checking if that percentage lies between 0.75 and 1.00

Related

Cumulative measure with a conditional reset

I have the need of creating a cumulative measure that resets when a certain condition happens.
The target is to create the 'Longest Stock Out Period' measure. That will be inserted in a matrix visual like this.
Date
No. of Negative Days
Longest Stock Out Period
08-03-2022
0
0
09-03-2022
1
1
10-03-2022
1
2
11-03-2022
0
0
The logic is that 'Longest stock out period' should cumulative sum 'No. of negative days' until 'No. of negative days' is 0, then it should reset.
This is what I have currently tried, which just computes a 0. I believe there should also be some logic in the measure regarding no. of negative days should not be 0 or alike.
Longest Stock Out =
VAR _date =
SELECTEDVALUE ( 'Calendar'[Date] )
RETURN
CALCULATE (
[No of Days],
FILTER ( ALL ( 'Calendar'[Date] ), 'Calendar'[Date] <= _date )
)
Try this Measure:
Longest Stock Out Period :=
VAR ThisDate =
MIN( 'Table'[Date] )
VAR DateOfLatestZero =
CALCULATE(
MAX( 'Table'[Date] ),
FILTER(
ALL( 'Table' ),
'Table'[Date] <= ThisDate
&& 'Table'[No. of Negative Days] = 0
)
)
RETURN
CALCULATE(
SUM( 'Table'[No. of Negative Days] ),
FILTER(
ALL( 'Table' ),
'Table'[Date] <= ThisDate
&& 'Table'[Date] >= DateOfLatestZero
)
)
Adapt so as to use your Calendar dates as required.

Force measure to display grand total (as average)

I have the below measure:
VAR VParent =
FILTER (
SUMMARIZE ( Revenue, 'Customer'[Parent], 'Cost'[Business] ),
[Measure1] > 0
)
VAR MaxParent = MAXX ( VParent, [Measure1] )
VAR SubT = CALCULATE ( [Measure1], VParent )
VAR VLead =
FILTER (
SUMMARIZE ( Revenue, 'Leads'[No], 'Cost'[Business] ),
[Measure1] > 0
)
VAR Max_Leads = MAXX ( VLead, [Measure1] )
VAR SubTotal_Leads = CALCULATE ( [Measure1], VLead )
RETURN
SWITCH (
TRUE (),
ISINSCOPE ( 'Customer'[Customer] ), DIVIDE ( SubT, MaxParent ),
ISINSCOPE ( 'Customer'[Parent] ), DIVIDE ( SubT, MaxParent ),
ISINSCOPE ( 'Leads'[Emp] ), DIVIDE ( SubTotal_Leads, Max_Leads ),
BLANK ()
)
If I select "table" visualization in Power BI, grand total appears BLANK()…
The measure as it is works fine, but I wanted to add a grand total whenever I display it in a table.
I am not sure if it is possible to display a grand total (as an average) of what is displayed.
A grand total will not have anything in scope, so the SWITCH function returns a blank, just as specified by your measure.

Trailing Twelve Months SUM not calculating in powerBI correctly

Trying to add a measure in PowerBI that calculates the rolling 12-month sum of sales and the measure works fine up until the most recent 12 months worth of data. Not sure what's causing this error. Below is the data and code in PowerBI I'm using.
TTM MRR =
CALCULATE (
SUM ( MRR[MONTHLY_REV] ),
FILTER (
ALL ( MRR[CLOSE_MONTH] ),
AND (
MRR[CLOSE_MONTH] <= MAX ( MRR[CLOSE_MONTH] ),
DATEADD ( MRR[CLOSE_MONTH], 1, YEAR ) > MAX ( MRR[CLOSE_MONTH] )
)
)
)
Data:
[Excel Data]
It might be easier with DATESINPERIOD.
TTM MRR =
VAR PeriodEnd = MAX ( MRR[CLOSE_MONTH] )
RETURN
CALCULATE (
SUM ( MRR[MONTHLY_REV] ),
DATESINPERIOD ( MRR[CLOSE_MONTH], PeriodEnd, -12, MONTH )
)

Calculate Column Sum of a Generated Table - DAX

I've been trying to get the sum of the following table's column VALUE using DaxStudio in order to put that on PowerBI once is's ok, since PBI can get slow if you test a code for large calculated tables.
Table from DaxStudio
BU VALUE
------------------------
FOODS 0.0000
FIBI 0.0000
GEOS/CIS 0.7300
CASC
S_S
SGS
COCOA
COCOA/SSSA
CORPORATE
N/A
CIS
The code behind it:
DEFINE
VAR TOTAL =
CALCULATE (
SUMX (
PACKAGING_POWERBI_YEARLY_2;
PACKAGING_POWERBI_YEARLY_2[QUANTIDADE_ANTERIOR]
);
FILTER (
PACKAGING_POWERBI_YEARLY_2;
PACKAGING_POWERBI_YEARLY_2[DATE] = "2018"
&& PACKAGING_POWERBI_YEARLY_2[CODIGO] = "43130"
)
)
EVALUATE
SUMMARIZE (
CALCULATETABLE (
FILTER (
PACKAGING_POWERBI_YEARLY_2;
PACKAGING_POWERBI_YEARLY_2[PRECO_PONDERADO] <> BLANK ()
&& PACKAGING_POWERBI_YEARLY_2[PRECO_PONDERADO] <> 0
)
);
PACKAGING_POWERBI_YEARLY_2[BU];
"VALUE"; FORMAT (
(
CALCULATE (
SUMX (
PACKAGING_POWERBI_YEARLY_2;
PACKAGING_POWERBI_YEARLY_2[QUANTIDADE_ANTERIOR]
);
FILTER (
PACKAGING_POWERBI_YEARLY_2;
PACKAGING_POWERBI_YEARLY_2[DATE] = "2018"
&& PACKAGING_POWERBI_YEARLY_2[CODIGO] = "43130"
&& PACKAGING_POWERBI_YEARLY_2[PRECO_PONDERADO] <> BLANK ()
)
) / TOTAL
)
* (
CALCULATE (
SUMX ( PACKAGING_POWERBI_YEARLY_2; PACKAGING_POWERBI_YEARLY_2[PRECO_PONDERADO] );
FILTER (
PACKAGING_POWERBI_YEARLY_2;
PACKAGING_POWERBI_YEARLY_2[DATE] = "2018"
&& PACKAGING_POWERBI_YEARLY_2[CODIGO] = "43130"
&& PACKAGING_POWERBI_YEARLY_2[PRECO_PONDERADO] <> BLANK ()
)
)
);
"0.0000"
)
)
Instead of generating the table I would like to sum the results of column VALUE, but the only way I got to plot a result without error is through a "CALCULATED TABLE" (above).
Every filter I try leads to an error.
The idea is to get just the sum of that calculated column VALUE:
0.7300
But every simpler filter or SUMX condition I try, an error pops-up.
I'd think you could clean it up to look something like this:
CALCULATE (
SUMX (
VALUES ( PACKAGING_POWERBI_YEARLY_2[BU] ),
SUM ( PACKAGING_POWERBI_YEARLY_2[QUANTIDADE_ANTERIOR] )
/ CALCULATE (
SUM ( PACKAGING_POWERBI_YEARLY_2[QUANTIDADE_ANTERIOR] ),
ALL ( PACKAGING_POWERBI_YEARLY_2[BU] )
)
* SUM ( PACKAGING_POWERBI_YEARLY_2[PRECO_PONDERADO] )
),
FILTER (
PACKAGING_POWERBI_YEARLY_2,
PACKAGING_POWERBI_YEARLY_2[DATE] = "2018"
&& PACKAGING_POWERBI_YEARLY_2[CODIGO] = "43130"
&& PACKAGING_POWERBI_YEARLY_2[PRECO_PONDERADO] <> BLANK ()
&& PACKAGING_POWERBI_YEARLY_2[PRECO_PONDERADO] <> 0
)
)
This uses SUMX to iterate through each of the BU values and for each one, calculates the value
(QUANTIDADE_ANTERIOR / Total QUANTIDADE_ANTERIOR ) * PRECO_PONDERADO
where the filters are reused instead of repeated.
I can't guarantee that this code works exactly as intended, but it should point you in a better direction.

How can I calculate a rolling average in DAX/PowerBI?

After going through several posts on StackOverflow and the PowerBI forums, I still can't figure out how to calculate a rolling average based on a given period- in my case, a 30-day rolling average.
Most of the posts I've seen advocate something either identical or really similar to this:
Rolling Sum :=
CALCULATE (
[Sales],
FILTER (
ALL ( Sales ),
[Date]
>= MAX ( Sales[Date] ) - 365
&& [Date] <= MAX ( Sales[Date] )
)
)
(code taken from this post)
...and yet, I can't seem to get the proper values.
In my case, I have the following:
"closing date" for a given loan (column)
loan count (measure)
closing length (column)- length of time (in days) to close a loan
What I'd like to calculate is the rolling 30 day average for any given day. I coded the following:
Rolling Average =
CALCULATE (
SUM(Query1[Closing_Length])/[Loan Count],
FILTER (
ALL ( Query1 ),
[Closing Date].[Date]
>= MAX ( Query1[Closing Date] ) - 30
&& [Closing Date] <= MAX ( Query1[Closing Date] )
)
)
To check the results, I used a visual filter to examine one month's worth of data and these are the results:
Note the totals row at the bottom; for this given period, there are 102 loans and it took an aggregate of 3922 days for them to close. The average I'd like to calculate is 3922/102, which should equal approximately 38.45 days. Instead, we see 42.
How can I fix this?
Measure based solution:
Rolling Average Measure =
VAR A =
SUMX (
FILTER (
ALL ( 'Query' ),
'Query'[Closing Date] <= MAX ( 'Query'[Closing Date] )
),
ROUND ( 'Query'[Closing Length], 2 )
)
VAR B =
SUMX (
FILTER (
ALL ( 'Query' ),
'Query'[Closing Date] <= MAX ( 'Query'[Closing Date] )
),
ROUND ( 'Query'[Loan Count], 2 )
)
RETURN
A / B
Calculated column based solution:
Rolling Average =
VAR CurrentDate = 'Query'[Closing Date]
VAR T =
FILTER ( 'Query', [Closing Date] <= CurrentDate )
RETURN
ROUND ( SUMX ( T, 'Query'[Closing Length] ) / SUMX ( T, [Loan Count] ), 2 )
Print Screen: