Power BI - Compare Measures over Period - time

I'm currently working on a PowerBI script, which gives me headaches. I introduced a measurement, which shows the amount of sales for every company in a corporation. Which basically looks like this.
MarketShare =
DIVIDE(
CALCULATE(
SUM('Monthly Reports'[Active Certificates])
),
CALCULATE(
SUM('Monthly Reports'[Active Certificates]),
ALL('Monthly Reports'[Institute])
)
)
The output of this is a beautiful matrix showing me the marketshare of every company compared to the total sales.
Now I should display in a matrix below, (or the same, even better) the change of market share for every company every month. Hence, July should display a blank cell since it has no data before. August should display in the first line +0,34 in September +0,3 ... etcetera for every entry in line one and continuing down.
I tried to implement a measure, which deducts always the values from the previous one, but I have not been successful so far.
Market Share Prev Period =
(
DIVIDE(
CALCULATE(
SUM('Monthly Reports'[Active Certificates])
),
CALCULATE(
SUM('Monthly Reports'[Active Certificates]),
ALL('Monthly Reports'[Institute])
)
)
)
-
(
DIVIDE(
CALCULATE(
SUM('Monthly Reports'[Active Certificates]),
DATEADD('Monthly Reports'[LoadDate], -1, MONTH)
),
CALCULATE(
SUM('Monthly Reports'[Active Certificates]),
DATEADD('Monthly Reports'[LoadDate], -1, MONTH),
ALL('Monthly Reports'[Institute])
)
)
)
I get weird results at the moment. If I choose to filter by date and add the entire date the following table is displayed, it shows the correct differences for the middle dates, however not for the final date:
When I filter by date hirarchy and months (like in the other table above) I get exactly the same results again.
Thanks,
Vincenz

In your calculation you use ALLEXCEPT.
ALLEXCEPT removes the filters from the expanded table specified in the first argument(remove from all column in this table), keeping only the filters in the columns specified in the following arguments. That means you keep context from your matrix (July from your header). conditions are mutually exclusive.
You want to do the opposite thing, remove the filter from your "date" column. Use ALL('TableName'[ColumnName])
Your first measure should looks like:
Simple =
DIVIDE(CALCULATE ( SUM ( 'Monthly Reports'[Sales] ) )
, CALCULATE (
SUM ( 'Monthly Reports'[Sales] ),
ALL ( 'Monthly Reports'[Company] )
))

I solved my calculation and found a workaround for the wrongly displayed date hierarchy.
Sometimes the answer to a question if you're not familiar with a programming language yet is simpler as one could expect.
Finished working code:
Market Share Prev Period =
IF(
CALCULATE(
SUM('Monthly Reports'[Sales]),
PREVIOUSMONTH('Monthly Reports'[SaleDate])
)
<> BLANK()
,
(
DIVIDE(
CALCULATE(
SUM('Monthly Reports'[Sales])
),
CALCULATE(
SUM('Monthly Reports'[Sales]),
ALL('Monthly Reports'[Company])
)
,
0
)
)
-
(
DIVIDE(
CALCULATE(
SUM('Monthly Reports'[Sales]),
PREVIOUSMONTH('Monthly Reports'[SaleDate])
),
CALCULATE(
SUM('Monthly Reports'[Sales]),
PREVIOUSMONTH('Monthly Reports'[SaleDate]),
ALL('Monthly Reports'[Company])
)
,
0
)
),
"-"
)
I figured out, that be using DateAdd(.., -1, Month) I only changed what was read, not in which column it goes. So I was always missing the most recent value, as off course it was not read by the -1 operation and I was missing the oldest report as there was none before off course.
Basically I found the function: PREVIOUSMONTH (too simple to be true).
Furthermore, I added an IF clause before to check if I'm at the first column. If so, I'm dashing the value.
My only problem, which was left in the end, was that when displaying the date hierarchy I only saw dashes in all the fields of the matrix. However, if I displayed the whole date I could see all correct values. My workaround was to format the date with "mmmm". This displayed the same. I hope I will not need the date in any further step, as I will need to come back to this again.
So my result looked exactly perfect.

Related

Parallel Period Explanation

I am fairly new to parallel periods and there use in dax. I understand the basics of parallel periods but I am not familiar using them. I am attempting to understand some code used by my company if anyone wouldn't mind breaking this down for me I would greatly appreciate it.
I understand the range it is pulling between but fail to understand how the initial max calendar year = 2021 works in all this. Also, if you can help explain how dividing the first set of code by the second works. That would be a freakin life saver. The end result currently is it comes out to equal a percentage that is used currently.
IF(
MAX('Calendar'[Year]) = 2021,
CALCULATE( [Total Cash] , PARALLELPERIOD('Calendar'[PERIOD_ENDING] , -24 , MONTH ) )
,
CALCULATE( [Total Cash] , PARALLELPERIOD('Calendar'[PERIOD_ENDING] , -12 , MONTH ) ))
IF(
MAX('Calendar'[Year]) = 2021,
CALCULATE( [Total Cash] , PARALLELPERIOD('Calendar'[PERIOD_ENDING] , -2 , YEAR ) )
,
CALCULATE( [Total Cash] , PARALLELPERIOD('Calendar'[PERIOD_ENDING] , -1 , YEAR ) ))

DAX code change from calculated column to a measure

I have a fact table with settlement_date, product_id, service_id, location_id, and ticket_id and srv_adjusted_earning columns.
I have determined the DAX query to generate a calculated column that sums the srv_adjusted_earning column over the date range: settlement date and settlement date - 27 days (i.e. a 4 week window) as:
=CALCULATE(
SUM(factService[SRV_ADJUSTED_EARNING]),
DATESBETWEEN
(
factService[SETTLEMENT_DATE],
DATEADD(factService[SETTLEMENT_DATE], -27, DAY),
factService[SETTLEMENT_DATE]
),
FILTER(factService, factService[PRO_ID] = EARLIER(factService[PRO_ID])),
FILTER(factService, factService[SER_ID] = EARLIER(factService[SER_ID])),
FILTER(factService, factService[LOC_ID_SELLING] =
EARLIER(factService[LOC_ID_SELLING])),
FILTER(factService, factService[TIS_ID] = EARLIER(factService[TIS_ID]))
)
I am trying to convert this DAX calculated column to a measure and I tried the following:
blob:=CALCULATE
(
SUM(factService[SRV_ADJUSTED_EARNING]),
DATESBETWEEN
(
factService[SETTLEMENT_DATE],
DATEADD(factService[SETTLEMENT_DATE], -27, DAY),
factService[SETTLEMENT_DATE]
),
ALLEXCEPT(factService, factService[PRO_ID]),
ALLEXCEPT(factService, factService[SER_ID]),
ALLEXCEPT(factService, factService[LOC_ID_SELLING]),
ALLEXCEPT(factService, factService[TIS_ID])
)
But I get:
Error: Calculation error in measure 'factService'[blob]: A single value for column 'SETTLEMENT_DATE' in table 'factService' cannot be determined. This can happen when a measure formula refers to a column that contains many values without specifying an aggregation such as min, max, count, or sum to get a single result.
Anybody know how I fix this?
As the error mentions, the issue is with factService[SETTLEMENT_DATE]. In the measure, there is no row context so that it knows which date you are talking about, so you need to specify it somehow. I'd suggest using a variable along these lines:
blob :=
VAR SettleDate = MAX ( factService[SETTLEMENT_DATE] )
RETURN
CALCULATE (
SUM ( factService[SRV_ADJUSTED_EARNING] ),
DATESBETWEEN (
factService[SETTLEMENT_DATE],
SettleDate - 27,
SettleDate
),
ALLEXCEPT (
factService,
factService[PRO_ID],
factService[SER_ID],
factService[LOC_ID_SELLING],
factService[TIS_ID]
)
)
Here the variable picks the maximal settlement date in the current filter context. If that's not exactly what you need, adjust the definition accordingly.

Issues on to calculate difference between previous quarter and current quarter

I am with the issue of difference between previous quarter, next quarter and current quarter. I have created date field and use the following calculation to resolve the issue but there is showing aggregated 4 quarters total instead of difference between last quarter and current quarter. The attached screenshot showing the grand total figure and quarter difference figure is same.
But the result is same. Only showing the total figure to sum all quarters. Please help me to find out the way to resolve the issue.
=IF (
FIRSTNONBLANK (
'10QuarterlyReportProgressPC'[Quarter], 1 ) = "Q1",
SUM ('10QuarterlyReportProgressPC'[Progress] ),
SUM( '10QuarterlyReportProgressPC'[Progress] )
- CALCULATE ( SUM ( '10QuarterlyReportProgressPC'[Progress]
),
PREVIOUSQUARTER ( '10QuarterlyReportProgressPC'[Start_QuarterYear] ) )
)

DAX ALLEXCEPT ignores Filter Context?

Need a Quarter-To-Date measure when a fiscal calendar is being used. Thought the following would work:
QTD =
CALCULATE (
[Total Amount],
FILTER (
ALLEXCEPT ( 'Calendar', 'Calendar'[FiscalYear] ),
'Calendar'[FiscalQuarter] = MIN ( 'Calendar'[FiscalQuarter] )
&& 'Calendar'[Date] <= MAX ( 'Calendar'[Date] )
)
)
However, even though the ALLEXCEPT function is saying NOT to "reset" the [FiscalYear] column, the measure is being calculated without regard to the fiscal year of the pivot coordinates. So the formula only works for the first fiscal year.
Anybody have any idea why the formula doesn't work as I anticipated?
TIA
Found a relevant thread on powerbi.com (link here), and I kinda sorta understand.
It seems that when you use the FILTER function with a date table it automatically applies an ALL() and you lose the filter context.
For my QTD formula the following works correctly:
CALCULATE(
[Total Amount],
FILTER(
ALL( 'Calendar' ),
'Calendar'[FiscalYear] = MIN( 'Calendar'[FiscalYear] )
&& 'Calendar'[FiscalQuarter] = MIN( 'Calendar'[FiscalQuarter] )
&& 'Calendar'[Date] <= MAX( 'Calendar'[Date] )
)
)
Please reply if you know of a simpler formula. The fiscal quarter ends, btw, do NOT align with the calendar quarter ends.

Filter Dax Calculation based upon YTD Sum

In a Tabular SSAS Model, I'm trying to count the number of distinct customers that purchased a given product wtihin a YTD Timeframe. The table contains measures that aren't explicit sums, so I get the Cartesian Product of all products for each customer, regardless of no sales. I'm attempting to limit the count by filtering out customer / product combinations with YTD Sales = 0. However, I cannot get the FILTER to recognize the DATESYTD context. It only ever filters based upon Sales existing within the chosen calendar month. I've tried inserting the ALL function every which way.
This is what I have so far.
Measure:
CALCULATE (
DISTINCTCOUNT ( Fact[Customer] ),
DATESYTD ( Calendar[Date] ),
FILTER ( Fact,
CALCULATE ( [Sum of Sales], DATESYTD ( Calendar[Date] ) ) <> 0
)
)
This measure will, for example, count distinct customers purchasing a product in Month #5 if Month #5 is explicitly chosen. It will not, however, include a customer that purchased that item in Month #2 of the same year.
I think the following DAX should do the trick:
COUNTROWS(
FILTER(
VALUES(Fact[Customer]),
CALCULATE ( [Sum of Sales], DATESYTD ( Calendar[Date] ) ) <> 0
)
)
Also, make sure your 'Calendar' table has been marked as a date table. If, for some reason, you prefer not to mark it as a date table, rewrite the above DAX to:
COUNTROWS(
FILTER(
VALUES(Fact[Customer]),
CALCULATE ( [Sum of Sales], DATESYTD ( Calendar[Date] ), ALL('Calendar') ) <> 0
)
)
Edit: Do you have records in your fact table where [Sum of Sales] is 0? If not, then you could simplify and improve the performance considerably by writing:
CALCULATE(
DISTINCTCOUNT(Fact[Customer]),
DATESYTD( Calendar[Date] )
)
Again, if you haven't marked your 'Calendar' table as a date table, add ALL(Calendar) to remove the filter on specific calendar columns.

Resources