Power Pivot Divide by 0 Percent Calculation Error in Excel - excel

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.

Related

SUMX, SUMMARIZE, and missing values

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.

excel pivot-table compute subtotal using dax

I am struggling with a "simple" function that is killing me. I am simply trying to get the subtotal of each group in a pivot table. I d like to compute the percentage of each subgroup as my input data are wrong. It seems I cannot do the classic EARLIER, and there is no this or current or whatever...
CALCULATE (
SUM ( table1[numbers] ),
FILTER (
ALL ( table1 ),
table1[Names] = FIRSTNONBLANK ( ALL ( table1[Names] ), tables[Names] )
)
)
Thanks
Found it! Absolutely counterintuitive to me, but it works...
=CALCULATE (
SUM ( table1[numbers] ),
ALLEXCEPT ( table1, table1[Names])
)

How I can find percentage out of subtotals with multiple columns in Pivot table

How I can find percentage out of subtotals with multiple columns in Pivot table.
PercentYes :=
CALCULATE ( SUM ( MyTable[value] ), MyTable[answers] = "yes" ) /
CALCULATE (
SUM ( MyTable[value] ),
ALL ( MyTable[subcategory], MyTable[answers] )
)
I created a sample of values
I would first create a sum measure:
Sum of Values:=SUM(MyTable[Value])
Then, I would create the percentage measure:
Percent of Values :=
DIVIDE (
[Sum of Values],
CALCULATE (
[Sum of Values],
ALL ( MyTable[Subcategory],MyTable[Answers] )
)
)
Using DIVIDE will help with error trapping zeroes in the denominator. The result looks like this:

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.

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