I am trying to calculate the gradient of the trendline passing through a series of points contained within my dataset. I have researched to see if there are built in functions to do this and there doesn't seem to be, so I am doing it manually. I'm not a DAX expert (nor probably a maths expert either!).
I have created a table in excel to walk through a simple example so I know what I'm aiming for:
In the Power BI environment, there are two tables joined on the "Month&Year" columns. An abridged illustration of these tables is below:
Please note the "Orders" measure from the illustration is referred to as "Special orders per day" in the Power BI code.
Step 1
Create the measure that averages the month numbers:
Average of months =
- AVERAGEX (
SUMMARIZE (
CALCULATETABLE ( Query_GSR, ALLSELECTED ( User_Friendly_Months ) ),
Query_GSR[Month&Year],
"AvMonths", AVERAGE ( Query_GSR[MonthNumForSlope] )
),
[AvMonths]
)
I use AVERAGE in the expression part so that the record for Sept-2018 has a 21 in the "AvMonths" column and then for Oct-2018 it says 22. I guess I could have used MIN or MAX because they will all say 21 or 22 depending on the month (only one to avoid would be SUM as this would add them all up).
I also tried to do this by summarizing and then creating a NATURALLEFTOUTERJOIN to the User_Friendly_Months table to get the month number for these months and when incorporating that into the rest of this procedure the measure took forever to calculate (even though it actually worked in the end somehow).
Step 2
Do the same for orders:
Average of special orders =
- AVERAGEX (
SUMMARIZE (
CALCULATETABLE ( Query_GSR, ALLSELECTED ( User_Friendly_Months ) ),
Query_GSR[Month&Year],
"Special OPD", [Special orders per day]
),
[Special OPD]
)
Step 3
Perform the calculation that goes through to step "C" in my original picture:
Column_C_Step =
SUMX (
SUMMARIZE (
CALCULATETABLE ( Query_GSR, ALLSELECTED ( User_Friendly_Months ) ),
Query_GSR[Month&Year],
"Special OPD", [Special orders per day],
"MonthNum", AVERAGE ( Query_GSR[MonthNumForSlope] )
),
( [Special OPD] + [Average special orders] )
* ( [MonthNum] + [Average of MonthNums] )
)
Instead of returning -11.95 in my example, the measure returns zero.
When I do this:
Check_orders_worked =
SUMX (
SUMMARIZE (
CALCULATETABLE ( Query_GSR, ALLSELECTED ( User_Friendly_Months ) ),
Query_GSR[Month&Year],
"Special OPD", [Special orders per day],
"MonthNum", AVERAGE ( Query_GSR[MonthNumForSlope] )
),
[Special OPD]
)
...I get 1188.9, which is the total of "Orders" in my Excel table illustration (so must be working).
When I do this:
Check_months_worked =
SUMX (
SUMMARIZE (
CALCULATETABLE ( Query_GSR, ALLSELECTED ( User_Friendly_Months ) ),
Query_GSR[Month&Year],
"Special OPD", [Special orders per day],
"MonthNum", AVERAGE ( Query_GSR[MonthNumForSlope] )
),
[MonthNum]
)
...I get 43, which is the total of Month_Num in my illustration (so again, must be working).
But when I attempt to perform the equivalent of a SUMPRODUCT on A and B to get C, it returns zero.
Can anyone shed any light on what on earth is going on??
It is driving me insane.
Or if there is a simpler way to do a gradient calculation I will cry with joy.
Thank you
UPDATE
For completeness here is the measure that worked:
Step_C_Measure =
VAR _OrdersAverage = [Average special orders]
VAR _MonthsAverage = [Average of MonthNums]
RETURN
SUMX (
SUMMARIZE (
CALCULATETABLE ( Query_GSR, ALLSELECTED ( User_Friendly_Months ) ),
Query_GSR[Month&Year],
"Special OPD", [Special orders per day],
"MonthNum", AVERAGE ( Query_GSR[MonthNumForSlope] )
),
( [Special OPD] + _OrdersAverage )
* ( [MonthNum] + _MonthsAverage )
)
Then Step D:
Step_D_Measure =
VAR _MonthsAverage = [Average of MonthNums]
RETURN
SUMX (
SUMMARIZE (
CALCULATETABLE ( Query_GSR, ALLSELECTED ( User_Friendly_Months ) ),
Query_GSR[Month&Year],
"Special OPD", [Special orders per day],
"MonthNum", AVERAGE ( Query_GSR[MonthNumForSlope] )
),
( [MonthNum] + _MonthsAverage )
* ( [MonthNum] + _MonthsAverage )
)
And finally to get the gradient:
Special order gradient =
DIVIDE ( Step_C_Measure, Step_D_Measure, "" )
In a question about multiple linear regression, I linked to a community post that covers basic linear regression.
In your case, the formula for the slope can be calculated similar to this:
Slope =
VAR RowCount = COUNTROWS(Query_GSR)
VAR Sum_X = SUMX(Query_GSR, Query_GSR[Month_Num])
VAR Sum_Y = SUMX(Query_GSR, Query_GSR[Orders])
VAR Sum_XY = SUMX(Query_GSR, Query_GSR[Month_Num] * Query_GSR[Orders])
VAR Sum_XX = SUMX(Query_GSR, Query_GSR[Month_Num] * Query_GSR[Month_Num])
RETURN DIVIDE(RowCount * Sum_XY - Sum_X * Sum_Y, RowCount * Sum_XX - Sum_X * Sum_X)
This works for a regression on multiple months, not just two.
Related
I have a DAX code that I know can be hugely improved on in terms of efficiency and performance but I'm not quite sure how to go about it.
Total GMRR (EUR) =
SUMX (
FILTER (
SUMMARIZE (
fact_transaction_monthly,
dim_partner[partner_created_date],
dim_partner[partner_name],
"GMRR", SUM ( fact_transaction_monthly[euroConsolidatedGMRR] ),
"check", [checkActive]
),
[check] = 1
),
[GMRR]
)
I am creating a summary table and summing over the values where the check is equal to 1 but this is taking a long time to compute
Check Active Code:
checkActive =
IF ([Total Active Partners] = 1,1,0)
Total GMRR (EUR) =
SUMX (
CALCULATETABLE (
SUMMARIZE (
fact_transaction_monthly,
dim_partner[partner_created_date],
dim_partner[partner_name],
"GMRR", SUM ( fact_transaction_monthly[euroConsolidatedGMRR] )
),
[checkActive] = 1
),
[GMRR]
)
I need to manipulate the Sales measure to exclude any transactions which have been reversed.
Sales measure is as follows right now:
Sales ($) =
IF (
HASONEVALUE ( 'Currency'[Detail] ),
SUMX (
'Sales',
'Sales'[Value_Sold]
* CALCULATE ( VALUES ( 'Exchange Rates'[ExchangeRate] ) )
)
)
What steps and where would I add to extract Reversed Transactions from this measure? I tried below as that's what you'd do in EXCEL but that is not working - when pulling results, it can't display anything on the visual
Sales - Reversals ($) =
IF (
HASONEVALUE ( 'Currency'[Detail] ),
SUMX (
'Sales',
'Sales'[Value_Sold]
* CALCULATE ( VALUES ( 'Exchange Rates'[ExchangeRate] ) ) - SUMX ('Sales', 'Sales [Trans_Type] = "Reversed")
)
)
)
If what you want is to exclude the reversed transactions from your first code, maybe this could help:
Sales ($) =
IF (
HASONEVALUE ( 'Currency'[Detail] ),
CALCULATE(
SUMX (
'Sales',
'Sales'[Value_Sold]
* CALCULATE (
VALUES ( 'Exchange Rates'[ExchangeRate] )
)
),
'Sales'[Trans_Type] <> "Reversed"
)
)
It's basically adding the SUMX into a CALCULATE function which let's you make the some applied to filters (in this case not reversed transactions).
I have three tables time, SectorProject, product Project.
Time AS(
SELECT *
FROM (VALUES(1011,48),(201,520),(36,120))V(id_project,time)),
SectorProject AS(
SELECT *
FROM (VALUES(1011,'BM'),(1011,'Fi'),(1011,'Om'),(201,'BM'),(36,'BM'))V(id_project,Sector1)),
prductProject AS(
SELECT *
FROM (VALUES(1011,'bike'),(1011,'velo'),(1011,'pc'),(201,'n'),(36, 'r' ))V(id_project,product))
I need to calculate average divided by the count per project
With sql server it will something like the following code:
avg( sum(time)) over (partition by t.id_project) * 1.0 / count(*) over (partition by t.id_project)
How to do the same thing within Dax.
You should look for "sum by category" to find the correct DAX pattern.
SumOverPartition:=
CALCULATE (
SUM ( T[Sales] ),
REMOVEFILTERS ( 'T' ),
VALUES ( T[Category] )
)
CountOverPartition:=
CALCULATE (
COUNTROWS ( T ),
REMOVEFILTERS ( 'T' ),
VALUES ( T[Category] )
)
You could also achieve it with ALLEXCEPT function but the pair REMOVEFILTERS and VALUE is better. Learnt the hard way.
https://www.sqlbi.com/articles/using-allexcept-versus-all-and-values/
In that article, they use the archaic name ALL instead of REMOVEFILTERS.
If you want it in calculated column, then you have to wrap it with another CALCULATE.
SumOverPartition:=
CALCUALTE
CALCULATE (
SUM ( T[Sales] ),
REMOVEFILTERS ( 'T' ),
VALUES ( T[Category] )
)
)
I'm new to Power BI and facing this issue with a Power BI report grand totals of these columns won't add up. Any help is much appreciated, below are the formulas
Rolling 3 Months =
CALCULATE (
SUM ( Deliveries[NetRevenue] ),
DATESBETWEEN (
Deliveries[DeliveryDate],
MAX ( Deliveries[DeliveryDate] ) - 90,
MAX ( Deliveries[DeliveryDate] )
)
)
Prior 3 Months =
CALCULATE (
SUM ( Deliveries[NetRevenue] ),
DATESBETWEEN (
Deliveries[DeliveryDate],
MAX ( Deliveries[DeliveryDate] ) - 180,
MAX ( Deliveries[DeliveryDate] ) - 90
)
)
Screenshot of the result
This is likely because MAX ( Deliveries[DeliveryDate] ) is not the same for every row.
The maximum is evaluated within the local filter context, not over the entire Deliveries table (or the subset of the table that matches your filter settings).
I'm guessing you probably want to define a variable to use as your date rather than calculating it (potentially) differently for each row in your matrix. E.g.
Prior 3 Months =
VAR LastDate =
CALCULATE ( MAX ( Deliveries[DeliveryDate] ), ALLSELECTED ( Deliveries ) )
RETURN
CALCULATE (
SUM ( Deliveries[NetRevenue] ),
DATESBETWEEN ( Deliveries[DeliveryDate], LastDate - 180, LastDate - 90 )
)
You might be able to use TODAY() instead of that LastDate calculation, depending on your particular situation.
I am using a measure below to display the months from fact table as described here:
Billings12Months =
CALCULATE (
SUM ( 'Datatable'[Allowable] ),
DATESINPERIOD ( DimDate[Date], MIN ( DimDate[Date] ), +12, MONTH )
)
My attempt to get the running total of above measure is failing:
BillingsRunningTotal =
CALCULATE (
[Billings12Months],
FILTER ( ALLSELECTED ( DimDate ), DimDate[Date] <= MAX ( DimDate[Date] ) )
)
BillingsRunningTotal2 =
SUMX (
FILTER (
ALLSELECTED ( DimDate[Date] ),
DimDate[Date] <= MAX ( ( DimDate[Date] ) )
&& YEAR ( DimDate[Date] ) = YEAR ( MIN ( DimDate[Date] ) )
),
[Billings12Months]
)
[BillingsRunningTotal] return same values as [Billings12Months] (please see screen 1 attached) and
[BillingsRunningTotal2] return wrong values and month start from Jan, 17 instead of May, 17 (please see screen-2)
Please help me to calculate the running total. If possible please describe how your solution is working so that I can be better in DAX.
Update:
Please see the screen-3 below for the output when I use the measure suggested by Kosuke:
BillingsRunningTotal =
CALCULATE (
SUM ( Datatable[Allowable] ),
FILTER ( ALLSELECTED ( DimDate ), DimDate[Date] <= MAX ( DimDate[Date] ) )
)
The months are from fact table (not from a Date table) and I think DATESINPERIOD plays a role to calculate and display the months. When we use SUM ( Datatable[Allowable] ), there would be a single month as dictated by the slicer. So we need to use DATESINPERIOD with rolling month calculation logic (DimDate[Date] <= MAX ( DimDate[Date] )) or virtually sum the [Billings12Months], It is where I am failing.
Thanks
You are almost there with the first attempt, however what to calculate is not [Billings12Months], but SUM( Datatable[Allowable] ).
BillingsRunningTotal =
CALCULATE (
SUM ( Datatable[Allowable] ),
FILTER ( ALLSELECTED ( DimDate ), DimDate[Date] <= MAX ( DimDate[Date] ) )
)
Essentially, [Billings12Months] and [BillingsRunningTotal] are same in calculating the sum of Datatable[Allowable], but the only difference is each measure calculates for different scope of period. Therefore, the right way of thinking is to wrap SUM ( Datatable[Allowable] ) in CALCULATE, with different filter parameters.