Calculating average NETWORK days Power BI - powerbi

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.

Related

How to find YTD daily average in Power Bi?

I am trying to figure out how to calculate a "New Measure" in my power BI visual that calculates the YTD daily average.
So for example,
my query on the backend would look like this
App Date | ID | Subject
01\01\2022 | 123 | Math
01\01\2022 | 456 | Science
01\02\2022 | 789 | Science
01\02\2022 | 012 | History
01\03\2022 | 345 | Science
01\03\2022 | 678 | History
01\03\2022 | 921 | Art
01\03\2022 | 223 | Science
01\04\2022 | 256 | English
Im trying to calculate what the daily average is YTD for math, science, history etc.
I tried
1 Daily average = calculate (average(Query[app date]))
I know this is not correct but I would appreciate any help..
Measure= AVERAGEX(VALUES(Date[APP_DATE]), [Subject])
This forumula should work to count YTD Average
First you need to create a date table using New Table and paste this code:
Date =
VAR ModelDate =
ADDCOLUMNS(
CALENDAR(DATE(2022,01,01), DATE(2022,12,31)),
"Year", YEAR([Date]),
"Month",FORMAT([Date],"mmm"),
"MonthNumber", MONTH([Date]))
RETURN
ModelDate
Then Create a relationship between date table and your fact table:
Then Go on to write this code AS Your measure:
Daily Avg_YTD =
CALCULATE(
AVERAGEX(
VALUES(YourTbl[Subject]),COUNT(YourTbl[Subject])),
DATESYTD('Date'[Date]))
Now If you put it on a table visual:
I hope It solves your problem!

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?

How do I calculated group averages with a filter in DAX?

I've got data across 2 tables that kind of looks like:
Table 1
Mean | Activity | name_id
---------------------------
1 | Swimming | 1
3 | Basketball | 2
3 | Swimming | 3
9 | Running | 1
5 | Basketball | 3
TypeName | Name_id
------------------
ABC | 1
DEF | 2
GHI | 3
Assuming joins are in place, I want to run the equivalent of this SQL:
select activity, avg(mean)
from table1 a
inner join table2
on table1.name_id = table2.name_id
where table2.name_id = 'DEF'
I got a basic quick measure that does everything except for the filter for 'DEF' name_ids:
Average =
AVERAGEX(
KEEPFILTERS(VALUES('Table1'[Activity])),
CALCULATE(SUM('Table1'[Mean]))
)
I'm stumped as to where exactly I should put a filter. I also tried using CALCULATE as the base function, but was stumped on how to properly run that.

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

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

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