How to find YTD daily average in Power Bi? - powerbi

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!

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?

Calculate YTD , MTD, WTD, Transaction Count in Power BI

I have 1 table with multiple rows. It looks something like this:
------------------------------------------------
StoreId| PostingDate | SalesAmt
MAIN | 2021-02-04 | 100
WEST | 2021-08-11 | 15
WEST | 2021-09-11 | 36
MAIN | 2021-11-11 | 78
MAIN | 2021-04-11 | 56
------------------------------------------------
And soon and so forth...
Now I want to produce the following in the Power BI as Table:
--------------------------------------------
StoreId| YTD | MTD | WTD | TransactionCount |
WEST |5,447| 800 | 74 | 1,475 |
MAIN |4,500| 421 | 15 | 1,855 |
--------------------------------------------
How can I achieve that? I am very new to this so I don't know how to do it.
I have been reading DAX and Power Query but maybe DAX is suitable for this?
I assumed your data looks like this. I've added data for the year 2022.
Also, I'm assuming there aren't future dates, the observations behave as transactions that happened in the past.
Table
StoreID
PostingDate
SalesAmt
WEST
16/01/2021
141
MAIN
24/01/2021
221
WEST
25/01/2021
119
MAIN
18/04/2021
209
MAIN
22/04/2021
220
MAIN
24/04/2021
167
WEST
16/11/2021
224
WEST
03/02/2022
155
MAIN
07/02/2022
236
WEST
11/02/2022
216
WEST
23/03/2022
135
MAIN
28/05/2022
153
WEST
01/06/2022
121
Calendar Table
For the calculations below to work, you need to create a calendar table.
It goes from the first date of Table until today.
If your calendar table is different the time intelligence function will not work.
Calendar = CALENDAR(MIN('Table'[PostingDate]),TODAY())
And mark the Calendar table as a Date Table.
Sales Amount
Sales Amount = sum('Table'[SalesAmt])
WTD
Assumes your week starts on Monday.
WTD =
VAR WeekStart = TODAY() - WEEKDAY(today(),2)
RETURN
CALCULATE([Sales Amount],'Table'[PostingDate]>=WeekStart)
MTD:
MTD =
TOTALMTD([Sales Amount],'Calendar'[Date])
YTD
YTD =
TOTALYTD([Sales Amount],'Calendar'[Date])

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.