Power BI/DAX Query - Finding value against range on another table - powerbi

So I have one table that has unique invoices as its rows, with a vendor name and an invoice cost among its columns, something like:
Invoice | Vendor | Cost
AAA | Good Co| $10
BBB | Good Co| $15
CCC | Best Co| $30
DDD | Bad Co | $50
And created a custom column to give me a total spend for each vendor:
VendorGrandTotal =
CALCULATE(SUM('Raw Data'[Cost]),ALLEXCEPT('Raw Data','Raw Data'[Vendor]))
To get a result like:
Invoice | Vendor | Cost | Total
AAA | Good Co| $10 | $25
BBB | Good Co| $15 | $25
CCC | Best Co| $30 | $30
DDD | Bad Co | $50 | $50
Meanwhile I have another table that depicts percentage of rebate from the vendor based on if the total spend with them is above or below a certain amount. Something like:
Vendor | Tier 1 % | Tier 1 From | Tier 1 To | Tier 2% | Tier 2 From
Good Co | 1% | $0 | $20 | 2% | $20
...and so on.
So in the case of this example we should be getting a rebate of 2% since the total spend of the invoices with Good Co adds up to greater than $20. But I'm stumped for how to execute this automatically within Power BI. Is there a way to produce a column or table somewhere that checks that the vendor in the invoices table is the same one from the rebate tier list AND can check against the different tier levels to see what percentage the rebate is AND return that percentage as a result that other calculations can be run against?

I'd suggest unpivoting your rebate table to look like this instead:
Vendor | Tier | From | To | Rebate
Good Co | 1 | $0 | $20 | 1%
Good Co | 2 | $20 | | 2%
Then you can look up the appropriate rebate percent by taking the maximal matching row where VendorGrandTotal is greater than From.
As a calculated column on 'Raw Data', you can do this:
Rebate % =
MAXX(
FILTER(Rebates,
Rebates[Vendor] = EARLIER([Vendor]) &&
Rebates[From] < EARLIER([VendorGrandTotal])
),
Rebates[Rebate]
)

Related

Power BI - Cannot figure out DAX logic to show dynamic last 6 months data as a tooltip

I have a finance PowerBI report that I'm trying to enhance, but struggling on this particular tooltip. Hopefully someone more knowledgable than me can help me out :)
Ok here's the relevant data model setup:
Table Name: "Date"
| Date | Month Year |
|:----------- |:------------:|
| 01/01/2022 | January 2022 |
| 02/01/2022 | January 2022 |
| 03/01/2022 | January 2022 |
etc.
Table Name: "Categories"
| Category |
|:------------------- |
| Household Shopping |
| Bills |
| Mortgage / Rent |
| Motoring |
| Payslip |
| Income |
etc.
Table Name: "Transactions"
| Date | Transaction | Category | Amount |
|:----------- |:-----------------|:-------------------|:-------|
| 05/01/2022 | Mortgage Payment | Mortgage / Rent | -£1000 |
| 05/01/2022 | Electricity Bill | Bills | -£80 |
| 08/01/2022 | Salary | Payslip | £2500 |
| 11/01/2022 | Grocery Shopping | Household Shopping | -£45 |
| 12/01/2022 | DIY Store | Household Shopping | -£170 |
| 12/01/2022 | Car Fuel | Motoring | -£80 |
etc.
And the model is Date --> Transactions <-- Category
I have a report that shows a snapshot of my finances and spending on a monthly basis, so there's a dropdown slicer at the top of the page where I can change the month (Date[Month Year]) and all the visuals update to show relevant data for that particular month.
I have these measures to calculate my earnings and spending:
Earnings =
CALCULATE (
SUM ( Transactions[Amount] ),
OR ( Transactions[Category] = "Payslip", Transactions[Category] = "Income" )
)
Spending =
( -1 )
* CALCULATE (
SUM ( Transactions[Amount] ),
Transactions[Category] <> "Payslip",
Transactions[Category] <> "Income"
)
Spending % of Earnings =
DIVIDE ( [Spending], CALCULATE ( [Earnings], ALL ( Categories ) ) )
On this page I have 2 bar charts that shows my spending for each category. These both have the y-axis as Categories[Category] and the value as the "Spending" and "Spending & of Earnings" measures respectively.
So far all works well.
Here's where I'm getting stuck. When I hover over the various bars, I would like to see a tooltip that shows a column/line chart of those measures over the past 6 months. For example if I selected June 2022 in the page slicer, I'd like the tooltip to show the spending measures for Jan 2022 - June 2022.
I've managed to get a tooltip that shows data for the past 6 months, and think it works for the 'Spending' measure. But for the life of me I can't get the 'Spending % of Earnings' working. I assume there's some combination of DAX to do it but I'm struggling.
Could someone please help me with this?

ALL() isn't working to "remove a filter" in DAX; relationship issue?

Basic premise:
'Orders' are comprised of items from multiple 'Zones'.
Customers can call in for 'Credits' (refunds) on botched 'Orders'.
There is a true many-to-many relationship here, because one order could have multiple credits called in at different times; similarly, a customer can call in once regarding multiple orders (generating only one credit memo).
'Credits' granularity is at the item level, i.e.
CREDIT | SO | ITEM | ZONE | CREDAMT
-------------------------------------------------------
42 | 1 | 56 | A | $6
42 | 1 | 52 | A | $8
42 | 1 | 62 | B | $20
42 | 2 | 56 | A | $12
'Order Details' granularity is at the zone level, i.e.
SO | ZONE | DOL_AMT
-------------------------------
1 | A | $50
1 | B | $20
1 | C | $100
2 | A | $26
I have a 'Zone' filter table that helps me sort things better and roll up into broader categories, i.e.
ZONE | TEMP | SORT
-------------------------------
A | DRY | 2
B | COLD | 3
C | DRY | 1
What I need:
I want a pair of visuals for a side by side comparison of order total by zone next to credit total by zone.
What's working:
The 'Credits' component is easy, CreditTotal = abs(sumx(Credits,Credits[CREDAMT])) with Zone as a legend item.
I have a ORDER component that works when the zone is in the credit memo
Order $ by Zone =
CALCULATE (
SUM ( 'Order Details'[DOL_AMT] ),
USERELATIONSHIP ( 'Order Details'[SO], Credits[SO] ),
ALL ( Credits[CreditCategory] )
)
My issue:
Zones that didn't have a credit against them won't show up. So instead of
CREDIT | ZONE | ORDER $ BY ZONE
----------------------------------
42 | A | $76
42 | B | $20
42 | C | $100
I get
CREDIT | ZONE | ORDER $ BY ZONE
----------------------------------
42 | A | $76
42 | B | $20
I have tried to remove this filter by tacking on ALL(Zones[Zone]) and/or ALL('Order Details'[Zone]), but it doesn't help, presumably because it is reporting "all zones" actually found in the 'Credits' table. I'm hoping there's some way to ask it to report all zones in the 'Order Details' table based upon SOs in the 'Credits' table.
In case it helps, here's how the relationships are structured; as an aside, I've tried mixing and matching various combinations of active/inactive, single vs. bidirectional filtering, etc., but the current configuration is the only one that seems to remotely work as desired.
I'm grateful for any suggestions; please let me know if anything is unclear. Thank you.
I was able to get it to work by using 'Order Details'[Zone] rather than Zones[Zone] in the table visual and this measure:
Order $ by Zone =
CALCULATE (
SUM ( 'Order Details'[DOL_AMT] ),
USERELATIONSHIP ( 'Order Details'[SO], Credits[SO] )
)
Notice that regardless of your measure, there is no row in Credits corresponding to zone C, so it doesn't know what to put in the CREDIT column unless you tell it exactly how.
If you remove the CREDIT dimension column, then you don't need to swap tables as I suggested above. You can just use the measure above and then write a new measure for the CREDIT column instead:
CreditValue =
CALCULATE(
VALUES(Credits[CREDIT]),
ALL(Credits),
Credits[SO] IN VALUES('Order Details'[SO])
)

Calculating average NETWORK days Power BI

I've been reading various threads and guides to performing a 'NETWORKDAYS' style calculation in Power BI but struggling to make it work.
I have a table like this:
Team | Meeting | Report
aaa | 1/1/2018 | 9/1/2018
aaa | 1/1/2018 | 7/1/2018
bbb | 1/1/2018 | 1/2/2018
bbb | 1/1/2018 |
ccc | 1/1/2018 | 3/3/2018
aaa | 1/1/2018 |
And I want to return the average days without weekends and holidays, something like this:
Team | average
aaa | 5 (10/2)
bbb | 23 (45/1)
ccc | 45 (45/1)
I have this function, which seems to work albeit clunkily, but I don't know how to remove the non-weekdays from the Date table:
AVERAGEX(FILTER(Planning,NOT(ISBLANK(Planning Actual_FinalReport]))),
(COUNTROWS(DATESBETWEEN(DateTable[Date],
Planning[Actual_ClosingMeeting],Planning [Actual_FinalReport]))
))
Where DateTable is:
Date | Weekday
5/1/2018| 1
6/1/2018| 0
7/1/2018| 0
and so on...
Essentially, I want to iterate over Planning (filtering out blanks in [Report]) and count the dates between Meeting and Report from the Dates table, filtered by Weekday = 1. It's the syntax to link the tables I think I'm struggling with.
Modifying your formula a bit, how about something like this?
Average = AVERAGEX(
FILTER(Planning,
NOT(ISBLANK(Planning[Actual_FinalReport]))),
COUNTROWS(
FILTER(DateTable,
DateTable[Date] IN DATESBETWEEN(
DateTable[Date],
Planning[Actual_ClosingMeeting],
Planning[Actual_FinalReport]) &&
DateTable[Weekday] = 1)))
I'm adding the Weekday filter at the end.

How to remove blank rows in a calculated Power BI table?

I have a days to report measure where I perform some calculation on each row for the numerator and then filter out blank rows for the denominator. Example table, code and result as follows:
Team | Meeting | Report
aaa | 1/1/2018 | 9/1/2018
aaa | 1/1/2018 | 7/1/2018
bbb | 1/1/2018 | 1/2/2018
bbb | 1/1/2018 |
ccc | 1/1/2018 | 3/3/2018
aaa | 1/1/2018 |
This is my function for the average days
CALCULATE(
AVERAGEX(Planning,Planning[Report]-Planning[Meeting]),
FILTER(Planning,NOT(ISBLANK(Planning[Report])))
)
And I'd like:
Team | average
aaa | 7 (14/2)
bbb | 31 (31/1)
ccc | 61 (61/1)
Function seems to work but I'm slightly paranoid about my (lack of) understanding of CALCULATE and FILTER than I may be doing something wrong?
Your function looks fine. The FILTER removes any rows with a blank Report value and then the AVERAGEX evaluates for just those rows.
FYI, for this construction, you don't necessarily need FILTER you can just write the following since CALCULATE supports basic filtering:
Average = CALCULATE(AVERAGEX(Planning, Planning[Report] - Planning[Meeting]),
NOT(ISBLANK(Planning[Report])))
Another way to do this is to use FILTER inside of AVERAGEX instead of using CALCULATE:
Average = AVERAGEX(FILTER(Planning, NOT(ISBLANK(Planning[Report]))),
Planning[Report] - Planning[Meeting])

% of Grand Total of a Measure that uses other Measures and is Crossfiltered

this seems so easy in my head but I haven't been able to get it for the last few hours....
I have a Table visualization that provides Cost by Hour using measures.
Category | Total Cost | Hours | Cost per Hour
A | 1000 | 10 | 100
B | 2000 | 100 | 20
C | 100 | 4 | 25
D | -500 | 100 | -5
Total | 2600 | 214 | 12.1495
For my purposes, I would also like to create a % of Grand Total of Cost per hour to add to a treechart visualization. However, if I simply add [Cost per Hour] to the treechart again and use the "quick clac" functionality on the field it would return 823.7% for the first record in the above table as (100/12.1495) = 8.2307. I would like this % of GT of Cost per Hour to use the total sum of the Cost per Hour column. Desired Result:
Category | Total Cost | Hours | Cost per Hour | % of Cost per Hour
A | 1000 | 10 | 100 | 71.4%
B | 2000 | 100 | 20 | 14.3%
C | 100 | 4 | 25 | 17.9%
D | -500 | 100 | -5 | -3.8%
Total | 2600 | 214 | 12.1495 | 100%
A few things to note that makes the application of any DAX challenging. All of the below Measures are filtered by multiple filter visualizations from Tables 1-5 and page level filters from Tables 1-5
The table visualization exists in Table1. Costs exist in Tables 2-5 and are related to Table1 using a Many-to-One Single Direction Filter Relationship.
[Total Cost] is a Measure that adds together values from 4 different tables. Eg:Total Cost = sum(table2[value])+sum(table3[value])+sum(table4[value])+sum(table5[value])
[Hours] is a Measure that adds together a column from a table and divides by the distinct count of records in that table. Eg:Hours = sum(table1[hours])/Distinctcount(table1[records])
[Cost per Hour] is a Measure consisting of two other measure.Cost per Hour = [Total Cost] / [Hours]
I sort of feel like this is similar to people wanting to add percentages to pie charts... I'm just trying to ascribe a real number to express the proportion displayed in the TreeChart visualization. I really hope that this is easier than it seems.
EDIT #alejandrozuleta:
Table1 is the original table from which tables 2-5 are referenced&created. An index number was assigned in Table1 and tables 2-5 are linked on this reference number. The reason that tables 2-5 exists separately is because they contain separate cost "types" and a join that occurs in these tables adds additional columns that are only applicable to specific costs types.... for example Table2 is Personnel Costs:
index | Category | Cost Type | Value | Age of Personnel
1 | A | Personnel | 1 | 33
and Table3 is Maintenance Costs:
index | Category | Cost Type | Value | Scheduled or UnScheduled Maint
2 | A | Maintenance | 5 | Scheduled
The if [Age of Personnel] existed in Table3 then it would have a "null" for any record of the Maintenance [Cost Type] vice-versa [Scheduled or UnScheduled Maint] would have a "null" if it existed in Table2. Because I don't want to have to deal with filter visualizations needing to select "(blanks)" for certain costs types the data relationship between these tables is a Many-to-One Single Direction Filter using [index] as the key.
EDIT2:
Working .pbix file with notional data and the data model I described is linked:
StackOverflow_GTofMeasure_Crosfilltered.pbix
I think this solution could work for you. Basically I've created two helper measures (which you don't have to show in your table):
CostPerHourHelper = SUMX(TableName,[Cost per Hour])
CostPerHourTotal = SUMX(ALL(TableName),[Cost per Hour])
Now you can create your % of Cost per Hour measure using this expression:
% Cost Per Hour = [CostPerHourHelper]/[CostPerHourTotal]
It should produce:
UPDATE:
Use ALLSELECTED() function to preserve the explicit filters you applied.
% Cost Per Hour = SUMX ( TableName, [Cost per Hour] )
/ SUMX ( ALLSELECTED ( TableName ), [Cost per Hour] )
Let me know if this helps.