How to use Calculate with a Measure that originally used VAR to get the result - nested

I am fairly new to DAX, but the end result I want to achieve is to calculate how many of below cancelations per bucket have been cancelled / total sales (Total sales is a measure I can quote in DAX - already created)
In other words I'd like to see VARs/Buckets divided as so
"Cancelled < 30 days" / [# Total sales]
"Cancelled 31-60 Days" / [#Total sales]
and so on.
Trying to create separate measure for each of the buckets like so:
0-30 Bucket =
CALCULATE (
[Cancellation buckets],
[Cancellation buckets] = "Cancelled <30 Days"
)
but getting an error message -> A function CALCULATE has been used in a True/False expression that is used as a table filter expression. This is not allowed.
Below is a bucketing measure that #RADO kindly helped with
Cancellation buckets =
VAR Cancellations = [# Cancellations]
RETURN
SWITCH (
TRUE (),
ISBLANK([# Cancellations]), BLANK(),
Cancellations <= 30, "Cancelled <30 Days",
Cancellations <- 60, "Cancelled 31-60 Days",
Cancellations <= 90, "Cancelled 61-90 Days",
Cancellations > 90, "Cancelled 90+"
)
I want to calculate that to then be able to divide the sum of these calc per total sales
Any help is muchly appreciated!

Related

DAX COUNTIF equivalent, based on aggregated results

I have been racking my brain on this, I have a report that I need to get a count of different thresholds from the result of a measure. My data is daily, but the report needs to be at the weekly level. When I have tried to do a = CALCULATE ( COUNT( AgentData[AgentName] ), FILTER ( ALLEXCEPT( AgentData, AgentData[Start of Week], AgentData[AgentName] ), [AHT] < 600 )
This seems to count all days in the week where [AHT] < 600, however I am looking for a count of all agents in the week where [AHT] < 600. AHT is calculated using the fields AgentData[HandleTime] / AgentData[Chats]
The result I'm looking for, as an aggregate of weekly data. Can this be done? Ideally, the time interval would be based on what is selected in the pivot, and would work if at the daily, weekly, monthly level.
Week
Total Agents
< 600
Week 1
100
25
Week 2
125
40
Sample data:
Date
Agent Name
HandleTime
Chats
5/1/2022
Bob
6000
10
5/2/2022
Bob
4800
11
5/1/2022
Lucia
5200
8
Total Agents is a distinctcount of AgentData[AgentName]
Remove AgentData[AgentName] from ALLEXCEPT, replace COUNT with TotalAgents measure (if that one should be used for) and it should work properly:
a =
CALCULATE (
[TotalAgents],
FILTER (
ALLEXCEPT( AgentData, AgentData[Start of Week] ),
[AHT] < 600
)
)

Sumifs with date difference in range

I have a spreadsheet with:
list of recurrent expenses
other lists of recurring stuff, not important
an estimation of inflow and outflow monthly
The estimation is calculated with the following criteria (in AND logic):
date is between "since" and "to" (if since is empty, it is always valid, and the same for "to")
the month is a multiple of "every month" starting from "since", i.e. the voice must be included if "data" = "since" + "every months" * x, where x is an integer number
I wanted to create a sumifs like the following (Italian Excel, sorry):
=SOMMA.PIÙ.SE('Uscite'!$E$2:$E$1048576;'Uscite'!$C$2:$C$1048576;"<="&'v3'!$A2;'Uscite'!$D$2:$D$1048576;">="&'v3'!$A2;?????)
where ????? is the missing piece: I considered using a DATEDIF with "m", in order to get the difference in months between cell Ax and 'Uscite'!Since, then verifying if this difference is a perfect multiple of "every months", but no clue how to do it.
Any suggestion?
Thanks for your time!
In english formulation, you could do:
=SUM( 'Entrate ricorrenti'!$E$2:$E$100 *
( ('Entrate ricorrenti'!$D$2:$D$100 >= 'v3'!A2) + ISBLANK('Entrate ricorrenti'!$D$2:$D$100) ) *
IFERROR( MOD( DATEDIF( 'Entrate ricorrenti'!$C$2:$C$100, 'v3'!A2, "M" ), 'Entrate ricorrenti'!$B$2:$B$100 ) = 0,
0 ) )
In Italian:
=SOMMA( 'Entrate ricorrenti'!$E$2:$E$100 *
( ('Entrate ricorrenti'!$D$2:$D$100 >= 'v3'!A2) + VAL.VUOTO('Entrate ricorrenti'!$D$2:$D$100) ) *
SE.ERRORE( RESTO( DATA.DIFF( 'Entrate ricorrenti'!$C$2:$C$100; 'v3'!A2; "M" ); 'Entrate ricorrenti'!$B$2:$B$100 ) = 0;
0 ) )
So, this uses the very nice dinosaur function that you taught me about today. I have never seen this except for DAX.
It simply filters out the payments that stopped before the target date, ('v3'!A2) by multiplying the payment values, ('Entrate ricorrenti'!$E$2:$E$100), times the boolean expression
(('Entrate ricorrenti'!$D$2:$D$100 >= 'v3'!A2) + ISBLANK('Entrate ricorrenti'!$D$2:$D$100))
that also checks to see if the To Date is blank. Then it multiplies that times the modulo of the DATEDIF in months by the Frequency in months. If that is zero, then a payment lands in that month. DATEDIF has the nice property that if the date difference is negative, it generates an N/A error. This allows us to wrap it in an IFERROR to replace that with zero - this prevents the formula from including payments that have not yet started.

Adding business days in a DAX time intelligence measure

I am writing a custom time intelligence measure in DAX to calculate a disbursement period which is 30 days + the following Friday from a payment. The solution below appears to work. However, I need to only include business/working days which prevents me from using the DATEADD() function.
How could I go about skipping the weekend days in the count of 30?
Expected Amount Disbursed =
CALCULATE([Total Agency Component Paid],
FILTER(ALL('Calendar'),
DATEADD('Calendar'[Date], 30,DAY) - MOD(DATEADD('Calendar'[Date], 30,DAY), 7) + 6 > MIN('Calendar'[Date]) &&
DATEADD('Calendar'[Date], 30,DAY) - MOD(DATEADD('Calendar'[Date], 30,DAY), 7) + 6 < MAX('Calendar'[Date])
)
An easy way to compute dates considering working days is to add a column to the Calendar table with the progressive number of working days. Assuming we have the column IsWorkingDay in our Calendar table this new calculated column can be defined as
WorkingDayNumber =
VAR CurrentDate = 'Calendar'[Date]
RETURN
SUMX(
FILTER(
'Calendar',
'Calendar'[Date] <= CurrentDate
),
'Calendar'[IsWorkingDay]
)
where IsWorkingDay contains 1 for working days or 0 for non working days.
Then to compute the date after 30 working days since the date in the current row context
VAR WorkingDayNumber = 'Calendar'[WorkingDayNumber]
VAR DatePlus30WorkingDays =
CALCULATE(
MIN( 'Calendar'[Date] ),
'Calendar'[IsWorkingDay] = 1,
'Calendar'[WorkingDayNumber] = WorkingDayNumber + 30,
REMOVEFILTERS( 'Calendar' )
)

SSAS Tabular / PowerBI - Huge memory consumption and slow response when using two calculation groups

I have created two calculation groups in an azure analysis ( B0 ) environment. The first one is a list of measures that represent a score card. Most lines use the SELECTEDMEASURE(), but a few of them have a hard measure selected. The other calculation group has some Time intelligence (YTD, Prior Year & variances in % ) as well as a split in Actual and Budget. This is what the result looks like if i put a simple Amount measure ( SUM('fact'[Amount]) ) in.
Result of Visual
The logic in the measures is not ground breaking, these are the elements of the time structure:
Period Actuals: CALCULATE(SELECTEDMEASURE();'Fact'[Version]="Actuals")
Period Budget: CALCULATE(SELECTEDMEASURE();'Fact'[Version]="Budget O")
Period Variance: CALCULATE(SELECTEDMEASURE();'Structure'[Structure]="Period Actuals") -CALCULATE(SELECTEDMEASURE();'Structure'[Structure]="Period Budget")
Period Variance % :Divide(
CALCULATE(SELECTEDMEASURE();'Structure'[Structure]="Period Variance") ;
CALCULATE(SELECTEDMEASURE();'Structure'[Structure]="Period Budget")
;0)
YTD Actuals: CALCULATE(SELECTEDMEASURE();DATESYTD('Calender'[Date];"12-31");'Structure'[Structure] = "Period Actuals")
YTD Budget: CALCULATE(SELECTEDMEASURE();DATESYTD('Calender'[Date];"12-31");'Structure'[Structure] = "Period Budget")
YTD Current Variance: CALCULATE(SELECTEDMEASURE();'Structure'[Structure]="YTD Actuals") - CALCULATE(SELECTEDMEASURE();'Structure'[Structure]="YTD Previous Year")
YTD Current Year: CALCULATE(SELECTEDMEASURE();'Structure'[Structure] = "YTD Actuals")
YTD Previous Year: CALCULATE(SELECTEDMEASURE();DATESYTD(SAMEPERIODLASTYEAR('Calender'[Date]);"12-31");'Structure'[Structure] = "Period Actuals")
YTD Variance: CALCULATE(SELECTEDMEASURE();'Structure'[Structure]="YTD Actuals") - CALCULATE(SELECTEDMEASURE();'Structure'[Structure]="YTD Budget")
YTD Variance %: Divide(
CALCULATE(SELECTEDMEASURE();'Structure'[Structure]="YTD Variance");
CALCULATE(SELECTEDMEASURE();'Structure'[Structure]="YTD Budget")
;0)
The measure in the P&L structure are alse quite simple, here are some examples:
Gross Margin WG : CALCULATE ( SELECTEDMEASURE(); GLAccount[Level03 code] = "PL00_00")*-1
Gross Margin WG - Cranes : CALCULATE ( SELECTEDMEASURE(); GLAccount[Level03 code] = "PL00_00";'Fact'[MachineType] = "LBCC")*-1
Quantity Cranes: CALCULATE ( sum('Fact'[Qty]); GLAccount[Level04 code] = "PL00_00_000";'Fact'[MachineType] = "LBCC")
The model is quite small, SQL Management studio estimates its size to about 250MB.
The issue is that when we are using one of the names calculation groups we get normal responds times ( 2 seconds at most ) but when we use both it can take up to several minutes to get a result in Power BI. Excel is faster, but still takes up to 30 seconds.
Meanwhile the memory consumption in AS goes up by several GB ?!?
This becomes worse when navigating ( filtering or adding dimensions ) up to the point where PowerBI just stops working.
We have tried changing the DAX formula's but we can figure out why it is using so much memory. Hope one of you can send us in the right direction.
Thanks!

DAX Calculate Year To Date - Year to Previous Month (year change)

Trying to figure out how to calculate the equivalent to YTD but up til previous month when year changes.
For example:
Date | Value
2018-10 | 100
2018-11 | 100
2018-12 | 100
2019-01 | 100
2019-02 | 100
2019-03 | 100
Results expected:
When 2019-03 is selected
YTD = 300 (accumulated from 2019-01- to 2019-03)
Previous month accumulated = 200 (accumulated from 2019-01 to 2019-02)
When 2019-01 is selected
YTD = 100
Previous month accumulated = 300 (accumulated from 2018-10 to 2018-12)
I've tried:
Value_Accum_PreviousMonth:=TOTALYTD([Value];Calendar[Date]) - [Value]
but with the change of year, it doesn't work
Any ideas?
Disclaimer - I created this solution using Power BI. The formula should transfer over to Excel/PowerPivot, but it may require some minor tweaks.
While the time intelligence functions of DAX are super useful, I seem to be drawn towards the manual approach of calculating YTD, prior year, etc. With that in mind, this is how I would solve your problem.
First, I would make a measure that is a simply sum of your Value column. Having this measure is just nice down the road; not an absolute necessity.
Total Value = SUM(Data[Value])
Next, for use to calculate YTD manually based of the prior month, we need to know two things, 1) what is the target month (i.e. the prior month) and 2) what is the year of that month.
If you are going to use those values anywhere else, I would put them into there own measure and use them here. If this is the only place they will be used, I like to use variables to calculate these kinds of values.
The first value we need is the selected (or max date in cases of no selection).
VAR SelectedDate = MAX(Data[Date])
With that date, we can calculate the TargetYear and TargetMonth. Both of them are simple IF statements to catch the January/December crossover.
VAR TargetYear = IF(MONTH(SelectedDate) = 1, YEAR(SelectedDate) - 1, YEAR(SelectedDate))
VAR TargetMonth = IF(MONTH(SelectedDate) = 1, 12, MONTH(SelectedDate) - 1)
Having all of the values we need, we write a CALCULATE statement that filters the data to the TargetYear and month is less than or equal to TargetMonth.
CALCULATE([Total Value], FILTER(ALL(Data), YEAR([Date]) = TargetYear && MONTH([Date]) <= TargetMonth))
Putting it all together looks like this.
Prior Month YTD =
VAR SelectedDate = MAX(Data[Date])
VAR TargetYear = IF(MONTH(SelectedDate) = 1, YEAR(SelectedDate) - 1, YEAR(SelectedDate))
VAR TargetMonth = IF(MONTH(SelectedDate) = 1, 12, MONTH(SelectedDate) - 1)
RETURN
CALCULATE([Total Value], FILTER(ALL(Data), YEAR([Date]) = TargetYear && MONTH([Date]) <= TargetMonth))

Resources