Before previous month measure in (DAX) Power BI - excel

I am struggling with DAX measure to get 2nd month from today, or in business terms, before previous month (so lets say May from now).
I am using this DAX:
Prev-2Months =
CALCULATE (
CALCULATE (
[Comp_Spend],
MONTH ( CCC[Date] )
= IF (
MONTH ( MAX ( CCC[Date] ) ) <= 2,
10 + MONTH ( MAX ( CCC[Date] ) ), // similar DAX is for Month
before (so June) with a
little tweak
MONTH ( MAX ( CCC[Date] ) ) - 2
),
YEAR ( CCC[Date] )
= IF (
MONTH ( MAX ( CCC[Date] ) ) <= 2,
YEAR ( MAX ( CCC[Date] ) - 1 ),
YEAR ( MAX ( CCC[Date] ) )
),
ALL ( V_Dim_Dates ),
KEEPFILTERS ( CCC[ClinicID] )
)
)
When it comes to February 2022 with slicer I am getting Blank values, assuming that Fiscal Year ends on 09/30. How can I solve this to not getting blanks for this "year transition" case?

Hmmm I guess I solved it using DATEADD, where my huge formula gives the same result.. :/
Prev-1M = CALCULATE ([Compliance_Spend],DATEADD(Commercial[Date],-1,MONTH),REMOVEFILTERS(V_Dim_Dates_Extended))
So I can easily adjust if I wanna 1 or 2 or 3 months to have as previous..
It is good to know that there are XYZ options in DAX how you can obtain the same thing.
Credits for suggesting: #Anonymous

Related

How to display blanks after 100% is reached? DAX measure

I have created a measure which calculates the percent of completion for each project. But my client wants it to display blanks after the project is completed, i.e. after the first 100%.
I already tried using an If function, but it is returning the same value for every month. I also looked online, but did not find a solution. Here is my %OfCompletion measure and the measure that it depends on.
% of Completion:=
VAR sproject =
IF ( HASONEVALUE ( Project[Project] ), VALUES ( Project[Project] ) )
RETURN
CALCULATE (
DIVIDE (
[S Expenses Running Total],
CALCULATE (
[Total Sales Costs] + [Total Sales Hours],
ALL ( Sales ),
Sales[Project] = sproject
)
),
Project[Classification] = "IN"
)
Expenses Running Total:= CALCULATE (
[Total Sales Costs] + [Total Sales Hours],
FILTER (
ALL ( Dates ),
Dates[Current Month Offset] <= MAX ( Dates[Current Month Offset] )
)
)
Example Values,
And a screenshot of my model.
This checks if current month completion is 100% AND prior month completion is also 100%, and returns BLANK - otherwise returns the actual completion value:
Monthly Completion Measure:=
IF (
[% of Completion] = 1 &&
CALCULATE (
[% of Completion],
PARALLELPERIOD (
Dates[Date],
-1,
MONTH
)
) = 1,
BLANK,
[% of Completion]
)

DAX Moving Average Reconciliation

Following tutorials I have a dax formula that calculates a 12 month moving average. However it seems slightly off from what users would be expecting.
12 Month Moving Average:=AVERAGEX (
FILTER (
ALL (dim_Calendar ),
dim_Calendar[Month_from_date_iso] > ( NEXTDAY ( SAMEPERIODLASTYEAR ( LASTDATE (dim_Calendar[Date_iso] ) ) ) ) && /* t1 */
dim_Calendar[Month_from_date_iso] <= MAX (dim_Calendar[Date_iso] ) /* t2 */
),[Total Month Sales])
Edit: adding Total Month Sales
Total Month Sales:=CALCULATE (
SUM([Sales Gross]),
FILTER (
ALL ( dim_Calendar),
dim_Calendar[Month_from_date_iso] = MAX (dim_Calendar[Month_from_date_iso] )
)
)
I would have expected Column F to match Column E.
Am I misunderstanding how AVERAGEX works or is this a rookie error?
Edit2: progress update. Demonstrating how the proposed solution calculates the overall average, not last 12 months.

two days before Previousday dax

I have added a measure to calculate difference to yesterday's figures however
when it is Monday's figure it returns blank instead of comparing against Friday's figure.
Is there a way of asking sales figure from two days ago, I tried deducting 2 as per
previous Day Sales:=CALCULATE(
sum(Sales[1]),
PREVIOUSDAY(Calender[Date]-2))
But it didn't work
Any suggestions please?
Thanks,
B
Assuming in your context is present Sales[Date] or Calendar[Date] column you can get the previous date with sales by using a FILTER:
Previous Day Sales :=
CALCULATE (
SUM ( Sales[Sales] ),
FILTER (
ALL ( Sales ),
Sales[Date]
= CALCULATE (
MAX ( Sales[Date] ),
FILTER (
ALL ( Sales ),
COUNTROWS ( FILTER ( Sales, EARLIER ( Sales[Date] ) < Sales[Date] ) )
)
)
)
)
This is an example in Power BI, but works in Power Pivot (Excel) too.
Hope it helps.

DAX Time Intelligence with Conditional Logic

I have a "sales" table with team ID, payment type and date (see example below):
I want to write a DAX formula that will "lookup" the prior month's sales amount for a "Monthly" team and the 3 month prior sales amount for a "Quarterly" team. The lookup also needs to be filtered by team ID, so the prior sales belong to the same team.
Here's an example of the desired output (Change in Sales) :
PriorMonthSales =CALCULATE(
SUM(Table[Sales]),
FILTER(
Table[TeamID]&&
Table[Date] - 1
)
PriorQuarterSales =CALCULATE(
SUM(Table[Sales]),
FILTER(
Table[TeamID]&&
Table[Date] - 3
)
PriorSales = IF(Table[PaymentType] = "Monthly",PriorMonthSales, PriorQuarterSales)
These formulas obviously don't produce the desired result, but I am sharing them here to show my approach to solving this. Perhaps, someone more knowledgeable here can assist with the correct syntax and logic.
Thanks!
To calculate the total of the previous month/quarter you can add a calculated column. However you need to change your month column to a date.
jan 16 => 1/1/2016
formula:
=
IF (
myTable[paymenttype] = "monthly",
CALCULATE (
SUM ( myTable[sales] ),
FILTER (
myTable,
myTable[TeamID] = EARLIER ( myTable[TeamID] )
&& DATEADD ( myTable[date], 1, MONTH ) = EARLIER ( myTable[date] )
&& myTable[paymenttype] = EARLIER ( myTable[paymenttype] )
)
),
CALCULATE (
SUM ( myTable[sales] ),
FILTER (
myTable,
myTable[TeamID] = EARLIER ( myTable[TeamID] )
&& DATEADD ( myTable[date], 3, MONTH ) = EARLIER ( myTable[date] )
&& myTable[paymenttype] = EARLIER ( myTable[paymenttype] )
)
)
)

how to get a year history of data in dax

IIF(SUM
(
[Calendar].[Month].CurrentMember.Lag(11) :
[Calendar].[Month].CurrentMember,
[Measures].[Qty]
) = 0, 0,
SUM
(
[Calendar].[Month].CurrentMember.Lag(11) :
[Calendar].[Month].CurrentMember,
[Measures].[Num]
) /
SUM
(
[Calendar].[Month].CurrentMember.Lag(11) :
[Calendar].[Month].CurrentMember,
[Measures].[Qty]
) )
This is formula from multi dimensional model i am trying to convert this MDX formula to DAX formula to use in Tubular model.
12 Month Avg :=
IF (
CALCULATE (
SUM ( [QTY] ),
FILTER (
ALL ( Calendar[Month] ),
Calendar[Month] - 11
= ( Calendar[Month] - 11 )
)
)
= 0,
BLANK (),
CALCULATE (
SUM ( [Num] ),
FILTER (
ALL ( Calendar[Month] ),
Calendar[Month] - 11
= ( Calendar[Month] - 11 )
)
)
/ CALCULATE (
SUM ( [QTY] ),
FILTER (
ALL ( Calendar[Month] ),
Calendar[Month] - 11
= ( Calendar[Month] - 11 )
)
)
)
So i made this DAX formula to convert MDX formula at the top. However it seem not working properly when i select month in pivot table. Those two formula don't match when i filtered by month. How can i resolve that problem?
1) Create three base measures:
TotalNum := SUM([Num])
TotalQty := SUM([Qty])
Avg := DIVIDE ( [TotalNum], [TotalQty], 0 )
2) Create a calculated measure to calculate the average over the prior year including the current selected month:
AvgLastYear:= CALCULATE (
[Avg] ,
DATESINPERIOD (
Calendar[Date] ,
MAX(Calendar[Date]),
-1, year
)
)
Explanation:
First, you don't need that divide by zero rigmarole, both MDX and DAX have a DIVIDE() function which handles that implicitly.
Second, with DAX, the idea is to build a base measure and then use CALCULATE() to shift the context of that measure as needed - in this case the time period.
Here we're looking at the current selected month (represented as MAX(Calendar[Date]), though you could use any aggregation function) and then using DATESINPERIOD() to choose a set of dates in our Calendar table which represent the time period T-1 year to the current month.

Resources