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.
Related
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
I have a somewhat complicated scenario that I'm trying to build out in Excel using PowerPivot. We have sales pipeline items that each represent a sales opportunity and, associated with each of those pipelines, a number of events. I have history tables for both pipelines and events and would like to use them to construct a model of how a given sales executive's entire pipeline set has changed over time.
The result set from my SQL query might look like this:
pipeline_id
date_last_mod
pipeline_event_id
amount
P-01
1/1/2020
PE-01
100
P-02
1/1/2020
PE-02
500
P-02
1/2/2020
P-02
1/2/2020
PE-02
1000
P-03
1/1/2020
PE-03
200
P-03
3/1/2020
PE-03
200
P-03
4/1/2020
PE-04
300
P-03
5/1/2020
PE-03
400
P-03
5/15/2020
PE-03
300
I also have a date table with the usual fields. What I'm trying to produce is a PivotTable that looks like the following. You can see it takes the most recent 'amount' value of each pipeline event and sums those up as of the end of each month.
Year
Month
amount
2020
1
800
2020
2
800
2020
3
800
2020
4
1100
2020
5
1200
It seems like the solution should be something with SUMX and LASTNONBLANK. I have a measure that looks like this:
TestRev:=VAR MaxDate =
MAX ( qryPipelineUS[date_last_mod] )
RETURN
SUMX( ADDCOLUMNS( SUMMARIZE( qryPipelineUS, qryPipelineUS[pipeline_id], qryPipelineUS[pipeline_event_id] ), "temp",
CALCULATE (
SUM ( qryPipelineUS[amount] ),
LASTNONBLANK( qryPipelineUS[date_last_mod], CALCULATE( SUM( qryPipelineUS[amount] ) ) ),
ALL( qryDateTable ),
ALLEXCEPT( qryPipelineUS, qryPipelineUS[pipeline_event_id] ),
qryPipelineUS[date_last_mod] <= MaxDate ) ), [temp] )
The problem with it seems to be that the SUMMARIZE function won't generate rows for pipeline_event_id's in months that don't have entries for them. Makes sense, really. Even when I try to do something with CROSSJOIN, I can generate a table with the ID's I need, but can't get it to generate data.
I'd really appreciate any suggestions as to how I should go about this. I've been beating my head against the problem for a couple weeks now and I'm not getting anywhere. Thanks in advance.
The key here is to do your date calculations on the date table rather than your fact table and to remove date filters on your SUMMARIZE table.
Here's how I'd adjust your measure:
TestRev =
VAR MaxDate =
MAX ( qryDateTable[Date] )
RETURN
SUMX (
ADDCOLUMNS (
CALCULATETABLE (
SUMMARIZE (
qryPipelineUS,
qryPipelineUS[pipeline_id],
qryPipelineUS[pipeline_event_id]
),
ALL ( qryDateTable )
),
"temp",
CALCULATE (
LASTNONBLANKVALUE (
qryPipelineUS[date_last_mod],
CALCULATE ( SUM ( qryPipelineUS[amount] ) )
),
qryDateTable[Date] <= MaxDate
)
),
[temp]
)
Edit: In Excel, LASTNONBLANKVALUE is not recognized so you can use this expanded version:
Test =
VAR MaxDate = MAX ( qryDateTable[Date] )
RETURN
SUMX (
ADDCOLUMNS (
CALCULATETABLE (
SUMMARIZE (
qryPipelineUS,
qryPipelineUS[pipeline_id],
qryPipelineUS[pipeline_event_id]
),
ALL ( qryDateTable )
),
"temp",
CALCULATE (
CALCULATE (
SUM ( qryPipelineUS[amount] ),
LASTNONBLANK (
qryPipelineUS[date_last_mod],
CALCULATE ( SUM ( qryPipelineUS[amount] ) )
)
),
qryDateTable[Date] <= MaxDate
)
),
[temp]
)
Note that the date filter is not included in the same internal CALCULATE expression as LASTNONBLANK. If you did that, the measure would not work the same way.
Good day to everyone, I am having a problem where I am creating a measure in Power Pivot Excel where the goal is to create a Accuracy Percentage based upon the division of 2 columns. Please see the attached picture below.
As you can see, dividing by 0 gives a 100 percent accuracy when it reality it should be blank and or not counted. The formula i am using for this percentage calculation is listed below.
=
IF (
ISBLANK (
1 - DIVIDE (
( DISTINCTCOUNT ( '03a Qry for Inaccurate Count'[KEY] ) ),
( DISTINCTCOUNT ( Std_MainData[KEY] ) )
)
),
0,
1 - DIVIDE (
( DISTINCTCOUNT ( '03a Qry for Inaccurate Count'[KEY] ) ),
( DISTINCTCOUNT ( Std_MainData[KEY] ) )
)
)
Please let me know if this is possible to fix, thanks.
I am struggling with what I think is a really silly problem.
I am trying to get "MTD target" values from an arbitrary date selection. I have a table 'out' which has many dimensions +date, as well as a target table which gives me a constant target value per day, for each month. My goal is to get the number of days selected per EACH month and multiply it by the corresponding daily target for the relevant month.
For example, month 1 daily target is 2, month 2 daily target is 4. I have 4 days in month 1, and 3 days in month 2. My cumulative target should be 2*4+3*2 = 14.
I can do this for a single month no problem. But as soon as I have a date range selected that crosses 2 or more months it all goes to hell.
Table 'out' has date, country, and other dimensions.
Table 'targets' has a year, month, and country dimensions.
I am trying to get some join and multiplication that is something like SUM(month_country * selected_days)
Here are the DAX measures I've tried:
mtd_inv_tgt :=
CALCULATE (
SUM ( targets[daily_spend] ),
FILTER (
targets,
targets[market] = FIRSTNONBLANK ( out[co_market], "" )
&& targets[yyyymm] >= MIN ( out[yyyymm] )
&& targets[yyyymm] <= MAX ( out[yyyymm] )
)
)
* DISTINCTCOUNT ( out[date] )
mtd_inv_tgt :=
SUMX (
FILTER (
targets,
targets[market] = FIRSTNONBLANK ( out[co_market], "" )
&& targets[yyyymm] >= MIN ( out[yyyymm] )
&& targets[yyyymm] <= MAX ( out[yyyymm] )
),
targets[daily_spend] * DISTINCTCOUNT ( out[date] )
)
This works fine if the dates selected belong to one month. If I select 2 months it will add the daily spend across 2 months and then multiply it by the number of dates covering the 2 months. Like from the above example it would be (2+3)*(4+2) = 30, which is obviously wrong.
The caveat is I can't use SUMX on the 'out' table because there are many records per date+country, whilst the targets are a single entry per month+country.
I think something similar to this should work:
mtd_inv_tgt :=
SUMX (
VALUES ( out[date] ),
LOOKUPVALUE (
targets[daily_spend],
targets[yyyymm], SELECTEDVALUE ( out[yyymm] )
)
)
This iterates over each distinct out[date] value in the current filter context and adds up the daily_spend for each of those dates that it looks up by matching on yyymm.
Iterating over only the yyymm might be more efficient:
mtd_inv_tgt :=
SUMX (
VALUES ( out[yyymm] ),
DISTINCTCOUNT ( out[date] )
* LOOKUPVALUE (
targets[daily_spend],
targets[yyyymm], EARLIER ( out[yyymm] )
)
)
Note: If these don't work as expected, please provide sample data to check against as I haven't actually tested these.
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] )
)
)
)