How to extract sum of sales that have been reversed DAX - powerbi

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).

Related

Same column Date difference based on other field DAX POWER BI

I want to calculate the date difference for all values in Field1 based on Field2 for example datediff(3/1/2020-2/1/2020) based on Field2 (c-b).
I have tried several ways but no luck so far.
there is no minimum mentioned in the question, so i assume that you are looking for the difference of the previous stages.
Date Difference =
DATEDIFF (
CALCULATE (
MAX ( 'Table'[Date] ),
FILTER (
ALL ( 'Table' ),
'Table'[Date] < EARLIER ( 'Table'[Date] )
&& EARLIER ( 'Table'[Field1] ) = 'Table'[Field1]
)
),
'Table'[Date],
DAY
)
You can obtain a solution by adding 2 calculated columns:
First Calculated Column:
MinDate =
CALCULATE (
MIN ( YourTable[Date] ),
ALLEXCEPT ( YourTable, YourTable[Field1] )
)
2nd Calculated Column:
Date_Difference =
DATEDIFF ( [MinDate], [Date], DAY )
End Result:

Filter summarizecolumns table within measure using slicer - DAX for Power Pivot

I have a table my_data with the following columns: CATEGORY, SUPPLIER and AMOUNT. I have created measures to calculate the total amount:
Total_Amount := SUM(my_data[AMOUNT])
and to do an ABC classification, I created a ranking:
Ranking:=IF (
ISBLANK ( [Total_Amount] ),
BLANK (),
RANKX (
FILTER (
ALL ( my_data[CATEGORY], my_data[SUPPLIER] ),
my_data[CATEGORY] = MAX ( my_data[CATEGORY] )
),
CALCULATE ( [Total_Amount] )
)
)
a Running Total:
Running_Total:=VAR current_rank = [Ranking]
RETURN
SUMX (
FILTER (
ALL ( my_data[SUPPLIER] ),
[Ranking] <= current_rank
),
[Total_Amount]
)
a Running Total %:
Running_Total(%):=DIVIDE (
[Running_Total],
SUMX ( ALL ( my_data[SUPPLIER] ), [Total_Amount] ),
BLANK ()
)
and the ABC classifier:
ABC_class:=IF (
ISBLANK ( [Total_Amount] ),
BLANK (),
SWITCH (
TRUE (),
[Running_Total(%)] <= [Class_A], "A",
[Running_Total(%)] <= [Class_B], "B",
"C"
)
)
Now, my problem. I have several slicers. Once of them is to choose A, B and/or C from the ABC classification. Note, that in my data there is no column with A, B or C data. The classification is only a measure. So I used a trick to be able to connect the slicer to my Pivot Table and that works fine. PROBLEM: I do not manage to get the right subtotals and grand totals for the following measure:
measure:=VAR Selected_Class =
ALLSELECTED ( ABC_table[Class] )
VAR Supplier_Class =
CALCULATE (
[ABC_class],
ALLEXCEPT (
my_data,
my_data[CATEGORY],
my_data[SUPPLIER],
Period_Table[YEAR]
)
)
RETURN
IF (
HASONEFILTER ( my_data[SUPPLIER] ),
IF (
CONTAINSROW ( Selected_Class, Supplier_Class ),
[Total_Amount],
BLANK ()
),
SUMX (
FILTER (
SUMMARIZECOLUMNS (
StockMvts[SUPPLIER],
"total", [Total_Purchased(EUR)],
"class", [ABC_TotPurchased_byCat&Sup]
),
CONTAINSROW(Selected_Class, [class])
),
[total]
)
)
This last measure doesn't work. It gives an error. The problem is in the SUMX for the subtotals and grand totals. How do I make it to sum only the values of [Total_Amount] for the [SUPPLIER] where the [ABC_class] measure results in one of the values selected in the ABC slicer?
Note, I'm using Power Pivot on Excel.
Thank you!

DAX Calculate not Totaling

Can anyone help me with this formula?
Calculate formula not totaling
Rev SWLY2 =
CALCULATE (
SUM ( 'Fact Table'[[Revenue Net]]] ),
FILTER (
ALL ( DateTable ),
DateTable[WeekID] = SELECTEDVALUE ( DateTable[WeekID] ) - 52
)
)
For the total, SELECTEDVALUE ( DateTable[WeekID] ) returns a blank because there are multiple weeks within the filter context.
Try using MAX in the case instead of SELECTEDVALUE.
Edit: I think this should work as you'd expect:
Rev SWLY2 =
CALCULATE (
SUM ( 'Fact Table'[[Revenue Net]]] ),
FILTER (
ALL ( DateTable ),
DateTable[WeekID] + 52 IN VALUES ( DateTable[WeekID] )
)
)
Note that VALUES is a list when there are more than one values.

Issue trying to calculate gradient using Power BI

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.

How do I calculate the sum of Value for the last 6 sprints using DAX

Problem:
I need a calculated measure in DAX that sums the Value column for the last 6 sprints. I am basing the last 6 sprints on the DimSprintEndDateKey in descending order.
Table structure in PowerBI
The DAX that I am using:
CALCULATE (
SUM ( factSprint[Value] ),
FILTER (
ALL ( factSprint ),
COUNTROWS (
topn(6,
FILTER (
factSprint,
EARLIEST( RELATED ( dimSprint[DimSprintEndDateKey] ) )
> RELATED ( dimSprint[DimSprintEndDateKey] )
),RELATED ( dimSprint[DimSprintEndDateKey] ), DESC
)
)
)
)
I am assuming that the relationship on your tables is between 'dimSprint'[dimSprintKey] and 'FactSprint'[dimSprintKey].
That being the case, this measure could work for you.
Total Value Last Six Sprints =
VAR endDateSprint =
LOOKUPVALUE (
'dimSprint'[dimSprintEndDateKey],
'dimSprint'[dimSprintKey], SELECTEDVALUE ( 'FactSprint'[dimSprintKey] )
)
VAR dimTableFiltered =
FILTER ( 'dimSprint', 'dimSprint'[dimSprintEndDateKey] <= endDateSprint )
RETURN
CALCULATE (
SUM ( 'FactSprint'[Value] ),
ALL ( 'FactSprint' ),
TOPN ( 6, dimTableFiltered, [dimSprintEndDateKey], DESC )
)
Use it on a matrix visual (or pivottable). Be sure to put 'FactSprint'[dimSprintKey] or 'FactSprint'[SprintPK] on Rows and [Total Value Last Six Sprints] on Values.