How to drill-through to see details of calculated measures? - powerbi

I have a large database containing order information. I created a detail table of order details, a very small version of it is below.
Date
Part No.
Origin
Destination
Cost
Quantity
1/29/2023
100
MIA
MCO
$500
500
1/29/2023
100
TLH
ATL
$450
500
1/30/2023
100
JFK
MIA
$700
500
2/1/2023
100
MCO
SFB
$700
500
I used DAX measures to create a matrix showing which locations were sending stock back to the originating plant after it has already shipped the item out.
This resulted in the following matrix:
Location with Inbound and Outbound
2
Inbound/Outbound Quantity
1,500
Inbound/Outbound % of Volume
75%
When selecting the '2' in the "Location with Inbound and Outbound" row, I would like to be able to see a detailed table of the movements that comprised those two movements.
Ideal Result table:
Date
Part No.
Origin
Destination
Cost
Quantity
1/29/2023
100
MIA
MCO
$500
500
1/30/2023
100
JFK
MIA
$700
500
2/1/2023
100
MCO
SFB
$700
500
How can this be accomplished using DAX or power query?

This is a bit of weird requirement but you can accomplish it as follows. I also don't think you actually want a drill through if you're happy for a Power Query solution and I should have probably done that in the first place. However, as we're still on DAX, the following measure will work in a table or aggregated.
Measure =
VAR o = ALL('Table'[Origin])
VAR d = ALL('Table'[Destination])
VAR i = INTERSECT(o, d)
RETURN
IF(HASONEVALUE('Table'[Date]),
SUMX(
ADDCOLUMNS(
SUMMARIZE('Table', 'Table'[Origin], 'Table'[Destination]),
"#x", IF('Table'[Destination] IN i || 'Table'[Origin] IN i, 1,0)
), [#x]
), COUNTROWS(i)
)
You can put a visual level filter on your table to only show rows when the value is 1.

Related

PowerBI, DAX - multiply DISTINCTCOUNT of repaired products over value of those products from another related table

I have two separate tables that are used to calculate total savings of repaired products.
Table_A is a result of my SQL Server query and contains information about all repairs performed on specific products:
Timestamp
Product_serialnumber
Product_name
Repair_reference
Repair_description
2022-10-02 11:52:36.330
Serial_A
Product_A
J1
Damaged
2022-10-02 11:52:37.343
Serial_A
Product_A
J3
Damaged
2022-10-02 11:52:37.720
Serial_A
Product_A
P5
Bent
2022-10-20 03:02:11.713
Serial_B
Product_A
J8
Dirty
2022-10-20 03:03:45.570
Serial_C
Product_B
P40
Damaged
2022-10-20 23:49:54.837
Serial_D
Product_C
P8
Dirty
2022-11-12 08:26:11.270
Serial_E
Product_A
J50
Bent
One Product_serialnumber can be repaired many times.
Table_B is very simple and created manually - it contains information how much each unit of specific product is worth.
Product_name
Value
Product_A
250
Product_B
250
Product_C
175
Both tables are joined with many to one cardinality with single cross filter direction running from Table_B to Table_A.
My task was to create visual to show how many Product_serialnumber s were repaired and how much we saved by repairing them. It is very important to perform DISTINCTCOUNT of Product_serialnumber , not COUNT of all repairs performed on them as 3 repairs on 1 Product_serialnumber worth 250 is still 250 saved, not 750.
In PowerBI I have one table with summary. To calculate savings I created following DAX measure in Table_A:
Measure = DISTINCTCOUNT('Table_A'[Product_serialnumber]) * MAX(Table_B[Value])
It gave me results below:
Product_name
Distinct count of Product_serialnumber
Value
Savings
Product_A
3
250
750
Product_B
10
250
2500
Product_C
25
175
4375
Total
38
9500
Each row has correct Savings value but Total is wrong. Instead of 9500, there should be 7625.
I figured out that MAX(Table_B[Value]) in my DAX Measure simulates 38*250. When I changed that to MIN, it was 38*175. So still wrong
To correct that, what I eventually did was I created New Column in Table_A:
RELATED_Value_from_table_b = RELATED(Table_B[Value])
Now each row in Table_A has correct Value based on Product_name.
Only then I created this measure in Table_A:
Saved = SUMX(
DISTINCT('Table_A'[Product_serialnumber]),
CALCULATE(MAX('Table_A'[RELATED_Value_from_table_b])))
So I got following result:
Product_name
Distinct count of Product_serialnumber
Value
Savings
Product_A
3
250
750
Product_B
10
250
2500
Product_C
25
175
4375
Total
38
7625
Total is finally correct.
Of course I found the solution to the task but I am not happy of how I did it and I believe there is more optimized way of doing it. I want to know it to be better in the future.

How to calculate ratio for every row in power bi?

Could someone please guide me through this situation?
I have a need to compute for every row the ratio between, volume of the row and sum of volumes of rows of a given transport type.
But after calculating this calculated column, I would like to use it in a table visualization, and be affected by any slicer of it...
Was I clear?
Here follows the example of rows:
Delivery Method Order Nr VOLUME Ratio
Air 50102258 9 33%
Sea 50091716 50 52%
Sea 50092425 47 48%
Air 50102257 18 67%
Here's your measure (a calculated column doesn't work here since you have to aggregate per method):
% Volume =
DIVIDE(
SUM('Table'[VOLUME]),
CALCULATE(
SUM('Table'[VOLUME]),
ALLEXCEPT('Table','Table'[Delivery Method])
)
)
If the answer was useful please accept it and give it an upvote.

How to remove blank rows in a Power BI Table

I run monthly reports to show month over month impact on our transportation costs. I need to have a formula that will remove blank rows when there is not a record in the current period or previous period.
Routing Current Month Previous Month
aaaa 100 150
bbbb 125
cccc 200 210
dddd 180
My formula for trying to deal with this is:
MoM PPV =
IF(OR(ISBLANK([$/Container]), ISBLANK([PREVIOUS MONTH $CONTAINER])),
BLANK(),
DIVIDE([OCEAN CONTAINER DIFFERENCE], 'GCF Ocean'[$/Container])
)
EDITED
If your measure returns BLANK(), then power bi desktop automatically remove rows.
Measure = CALCULATE( DIVIDE(sum('Table'[Current]), sum('Table'[Previous])) )
example:
EDITED:
SumOfPrev = CALCULATE(SUM('Table'[Previous]))
SumOfCurrent = CALCULATE(SUM('Table'[Current]))
Measure = CALCULATE( DIVIDE([SumOfCurrent], [SumOfPrev]) )
You can try with visual level filter as shown in attached image. It will remove all the blank record which your measure "MoM PPV" code return.

Power BI: Multiply Grouping by Related Parameter

I'm fairly new to Power BI, so may be overlooking something basic here.
I have a Column which contains a banding number (1 to 3), depending on a customer's total spend.
Band 1: 0 - 500
Band 2: 501 - 1000
Band 3: 1001+
I have created 3 Parameters on my report (Parameter 1, 2 and 3).
Each parameter allows for a decimal range between 5 and 35 with increments of 2.5
This parameter will represent the percentage of the income that is earned by a consultant.
I'm trying to create a measure that multiplies the Income by the respective Parameter. Allowing users to adjust the percentage earning dynamically when viewing the report.
Below is an example of the table.
Table Name: Receipts
Policy Number
Client Name
Band
Income
SA1
Ray Mann
3
800
SA2
Ray Mann
3
900
SA3
Mary Yu
2
600
SA4
Sam Fry
1
20
SA5
Sam Fry
1
50
I'm a little lost on this one.
As a calculated columns I would do a simple SWITCH statement, however as it's not a measure it will not adjust dynamically as the parameter slider is changed.
Your help is greatly appreciated.
Kind regards,
Morallis
Let's say there are three what-if parameters, named Parameter1, Parameter2 and Parameter3, and your table is named Table. We can create a measure, which first will use SUMMARIZE to summarize the data calculating the total income per band:
SUMMARIZE('Table', 'Table'[Band], "Income", SUM('Table'[Income]))
Then will use SUMX to calculate the multiplied income:
Multiplied Income =
SUMX(
SUMMARIZE('Table', 'Table'[Band], "Income", SUM('Table'[Income])),
[Income] * SWITCH([Band],
1, Parameter1[Parameter1 Value],
2, Parameter2[Parameter2 Value],
3, Parameter3[Parameter3 Value],
1)
)
The result will be as follows:

Calculate total variance in power bi with dax

I have 3 measures:
1) total_trx = SUM(mytable[trx])
2) trx_prev_month = CALCULATE([total_trx], DATEADD(calendar[date], -1,MONTH))
3) monthly_var = DIVIDE([total_trx],[trx_prev_month])-1
If I add a waterfall viz, x-axis with month, it gives me the % of monthly variation and a TOTAL bar at the end that sums all the variations.
I need to reproduce that total number in order to show a KPI as in "so far, we've increased ...%", changing when using a date slicer.
Seems like sum(monthly_var) is not allowed.
Any ideas?
Thank you very much.
Edit1: sample with date filter = Last 4 months
Jul 100 0%
Aug 110 10%
Sep 90 -20%
Oct 80 -10%
Total: -20% <- need a dax to calculate this number and show just -20%
Then if I change the filter to, for example LAST 6 MONTHS, I need to calculate it up to May
In order to get the desired result we will use an intermediate table in our query that will summarize the results by months:
use this code and replace calendar[Year Month] with your Year month column :
SUMX(
SUMMARIZECOLUMNS(calendar[Year Month],"Monthly_int_var",[monthly_var]),
[Monthly_int_var]
)