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

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?

Related

PowerBI Ranking based on multiple columns

i am writing a dax formula to rank my products based on multiple column
input from 3 different tables
channel table - channel column
fruits table - brand, fruits, serving size columns
sales table - total sales column (calculated)
channel | brand | fruits | serving size | Total Sales
online | sunny | banana | slice| 100
shop | sunny | apple |whole| 90
amazon | bert | banana | whole | 89
shop | crystal | strawberry | slice|56
output table
channel | brand | fruits | serving size | Total Sales | Fruit_Rank
online | sunny | banana | slice| 100 | 1
shop | sunny | apple |whole| 90 | 2
amazon | bert | banana | whole | 89 | 3
shop | crystal | strawberry | slice|56 | 4
my current dax is
Fruit_Rank = RANKX(CROSSJOIN(ALL('Dim Channel'[channel]),ALL('Dim Fruits'[brand]),ALL('Dim Fruits'[fruits]),ALL('Dim Fruits'[serving size])),[Total Sales])
it gave me the wrong ranking.
Any help is appreciated!
thank you!

How do I repeat row labels in a matrix?

I have data showing me the dates grouped like this:
For security reasons, I had to remove the Customer Description detail, due to confidentiality.
How do I repeat the date column the same way you repeat the Row Labels in an Excel Pivot?
I've looked, but couldn't find a solution to this - this option should be available.
EDIT
When you have the following source data in Excel:
Date | Customer | Item Description | Qty Out | Unit Price | Sales
--------------------------------------------------------------------------------------------------------------------------------------------
14/08/2020 | Customer 1 | Item 11 | 4.00 | 65.00 | 260.00
14/08/2020 | Customer 2 | Item 12 | 56.00 | 12.00 | 672.00
14/08/2020 | Customer 3 | Item 13 | 64.00 | 35.00 | 2,240.00
14/08/2020 | Customer 4 | Item 14 | 29.00 | 65.00 | 1,885.00
15/08/2020 | Customer 2 | Item 15 | 746.00 | 12.00 | 8,952.00
15/08/2020 | Customer 3 | Item 16 | 14.00 | 75.00 | 1,050.00
15/08/2020 | Customer 4 | Item 17 | 45.00 | 741.00 | 33,345.00
15/08/2020 | Customer 5 | Item 18 | 456.00 | 125.00 | 57,000.00
15/08/2020 | Customer 6 | Item 19 | 925.00 | 17.00 | 15,725.00
16/08/2020 | Customer 4 | Item 20 | 6.00 | 532.00 | 3,192.00
16/08/2020 | Customer 5 | Item 21 | 56.00 | 94.00 | 5,264.00
16/08/2020 | Customer 6 | Item 22 | 546.00 | 37.00 | 20,202.00
You then pivot this data using Microsoft Excel, where you get the following:
You then choose the option to Repeat Item Labels as can be seen below:
After selecting this, you get my expected results I require in Power BI:
Is there not a function available like this in Power BI?
Just adding this for your reference as a work around. Check this below image with a custom column created in the Power Query Editor-
date_customer = Date.ToText([Date]) &" : "& [Customer]
Then added both Date and date_customer in the Matrix row level. The output is as below- (using your sample data)
ANOTHER OPTION Another option is to add Date and Customer in the Matrix row and the output is will be as below- (using your sample data)
This is also a meaningful output as date are showing as a group header. But in case of requirement of having redundant date to show, you can consider the first option.

How to duplicate values from grouped measures into additional virtual groups

I have a table with places and corresponding population per year. Currently I only have data up to year 2018. Somethin like this:
+-------+------+------------+
| Place | Year | Population |
+-------+------+------------+
| a | 2017 | 12 |
| a | 2018 | 11 |
| b | 2017 | 43 |
| b | 2018 | 21 |
+-------+------+------------+
I've created a measure that sums the "Population" column values and used it in a columns chart with the "Year" column as the axis to see the total population per year. The data in the chart would be:
+------+-------+
| Year | Total |
+------+-------+
| 2017 | 55 |
| 2018 | 33 |
+------+-------+
I need the chart to also show years 2019 and 2020 with the same value from 2020, like:
+------+-------+
| Year | Total |
+------+-------+
| 2017 | 55 |
| 2018 | 33 |
| 2019 | 33 |
| 2020 | 33 |
+------+-------+
How can I achieve the above via DAX. I basically need to take the "Total" from the MAX "Year" and dynamically create two additional entries for 2019 and 2020 with the same total from that MAX "Year".
My use case is more involved as then I will have to add another measure to the same columns chart to show how many of that total population attended a particular class. My table with the "class attendance" data does have values up to 2020 which is why I need the total population to go all the way to 2020 and assume that the population for 2019 and 2020 is the same than in 2018 so then I can compare that with class attendance numbers.
Any help or guidance is welcomed.
Thanks.
This can be achieved very easily by power query: (may be there will be a way to do it in measures as well)
Assuming you data would be something like this:
You can go to Transform tab and perform group by like below
Now Select the Fill(Down) in the Transform tab like below
Finally you can in the visualization tab you can create below:

Calculated Column compare to measure

yet another Power BI problem I'm having that I've tried to solve to no avail..
I'm trying to create a calculated column to flag if a record is in this year.
I have a planning table:
FiscalYear | ReportDate | Status |
2017 | 01/4/2018 | Open |
2018 | | Open |
2017 | | Closed |
2016 | 01/10/2016 | Complete |
2016 | 01/8/2017 | Complete |
I have a measure [Current Year] that returns 2018/19 as text.
I have a date table with every date, sample below of selected years:
FiscalYear | FiscalYearSlash| Date |
2017 | 2017/18 | ..........|
2018 | 2018/19 | ..........|
2017 | 2017/18 | ..........|
2016 | 2016/17 | ..........|
2016 | 2016/17 | ..........|
I can return FiscalYearSlash in the planning table with:
CALCULATE(VALUES(Dates[FiscalYearSlash]),
FILTER(Dates,
Dates[FiscalYear]=Planning[FiscalYear]))
but when I put this as the condition in an IF statement, it comes out false every time. Both the column and the measure are text. Any ideas?
Wondering if it's something to do with context transition. I've watched some of the SQLBI.com videos and understand them but when it comes to applying it.. struggling a bit. Thanks.
Here's the IF statement I'm trying:
IF( CALCULATE(VALUES(Dates[FiscalYearSlash]),
FILTER(Dates,
Dates[FiscalYear]=Planning[FiscalYear]))
= CurrentYear,"yes","no")
I've solved this. I returned the current year as a VAR in the function, as opposed to a measure:
VAR CurrentYear = CALCULATE(VALUES(Dates[FiscalYearSlash]),
FILTER(Dates,Dates[Date]=TODAY()))
RETURN
SWITCH(TRUE(),
CALCULATE(VALUES(Dates[FiscalYearSlash]),
FILTER(Dates,
Dates[FiscalYear]=Planning[FiscalYear]))=CurrentYear,
"Yes","No"
)

Total/Sum working incorrectly in Power Bi

I have created a Report in which I have created some measures like -
X =
CALCULATE (
DISTINCTCOUNT ( ActivityNew[Name] ),
FILTER (
ActivityNew,
ActivityNew[Resource Owner Name] = MAX ( 'Resource Owners'[Manager Name] )
&& ActivityNew[LocationId] = 2
)
)
When I use this measure in table then the column values dont add up. For eg. if the value of this measure is 2,2,2,2,2 then Total in table should be 10. but it is showing 2.
I have noticed that wherever I have used this MAX(), the measure values are not adding up.
Why this is happening and Is their any solution for this?
You are using DISTINCTCOUNT which is in general not aggregatable.
Say you have the following table Sales:
+----------+------+-------+
| Customer | Item | Count |
+----------+------+-------+
| Albert | Coke | 3 |
| Bertram | Beer | 5 |
| Bertram | Coke | 2 |
| Charlie | Beer | 1 |
+----------+------+-------+
If you wanted to count the number of distinct items each customer has bought, you would create a new measure with the formula:
[Distinct Items] := DISTINCTCOUNT(Sales[Item])
If you include the [Customer] column and your [Distinct Items] measure in a report, it would output the following:
+----------+----------------+
| Customer | Distinct Items |
+----------+----------------+
| Albert | 1 |
| Bertram | 2 |
| Charlie | 1 |
+----------+----------------+
| Total | 2 |
+----------+----------------+
As you can see, this does not sum up, as the context of the total row, is the entire table, not filtered by any particular customer. To change this behaviour, you have to explicitly tell your measure that it should sum the values derived at the customer level. To do this, use the SUMX function. In my example, the measure formula should be changed like this:
[Distinct Items] := SUMX(VALUES(Sales[Customer]), DISTINCTCOUNT(Sales[Item]))
As I only want to sum over unique customers I use VALUES(Sales[Customer]). If you want to sum over every row in the table simply do: SUMX(<table name>, <expression>).
With this change, the output in the above example would be:
+----------+----------------+
| Customer | Distinct Items |
+----------+----------------+
| Albert | 1 |
| Bertram | 2 |
| Charlie | 1 |
+----------+----------------+
| Total | 4 |
+----------+----------------+