Calculated Column compare to measure - powerbi

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

Related

how to make a query in sql to use in c++ code on counting the borrowing report in every month

I want to make a query to count borrowing report every month . But i'd saved my data in unixtime.
tablename:borrow
attributes:borrowingID,dateOfBorrow,dateOfReturn,statusBook
For example the dateOfBorrow is 167077440 and i just want to count the specific month for jan,feb,etc..
i am expecting
| Month | Total |
| ------| ----- |
| Jan | 2 |
| Feb | 5 |
| Mar | 5 |
...etc
select from_unixtime(167077440),from_unixtime(167077440,'%b')
+--------------------------+-------------------------------+
| from_unixtime(167077440) | from_unixtime(167077440,'%b') |
+--------------------------+-------------------------------+
| 1975-04-18 19:24:00 | Apr |
+--------------------------+-------------------------------+
1 row in set (0.001 sec)
See manual https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_from-unixtime and https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-format
But are you really interested in 1975?

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 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:

PowerBI Sort Columns in Matrix Visual

I have a Matrix visual in Microsoft PowerBI with Australian 'States' as rows and 'Months Ago' as columns.
By default the Matrix shows my columns from 0 months ago to 12. I would like it to show from 12 months ago on the left to 0 months ago on the right.
+-------------------+-----------------------------+-------+
| | Months Ago | |
+-------------------+-----------------------------+-------+
| State | 0 | 1 | 2 | 3 | 4 | 5 | Total |
+-------------------+----+----+----+----+----+----+-------+
| Queensland | 10 | 10 | 10 | 10 | 10 | 10 | 60 |
+-------------------+----+----+----+----+----+----+-------+
| New South Wales | | | | | | | |
+-------------------+----+----+----+----+----+----+-------+
| Victoria | | | | | | | |
+-------------------+----+----+----+----+----+----+-------+
| South Australia | | | | | | | |
+-------------------+----+----+----+----+----+----+-------+
| Western Australia | | | | | | | |
+-------------------+----+----+----+----+----+----+-------+
Currently I am only given the option to sort by the value type fields (ie revenue etc).
Is there any option to sort/order the Column Headers?
I don't think there is an option for you to sort column headers directly.
However, you can change the default sort order for the Months Ago column so that it will be reflected in general.
You can add a custom column MonthSrt = 12 - [Months Ago] in query editor:
(It won't work in DAX because of a known issue)
Then you can select the Months Ago column and sort it by MonthSrt:
The custom sort will be applied when you use the Months Ago column in visuals:
You can also make groups (1 to 1 items) al give them al logical number:
The order will change automaticly in the matrix
The following solution worked for me to display the dates in descending order in a matrix:
how to sort column dates in descending order of matrix in power bi

Display distinct text after user has selected a set of filters

I'm currently using the following code to display a text with a Long Text Viewer when the user selects a Country from the slicer:
Notes =
IF(
HASONEFILTER(Data[Country]),
CONCATENATEX(Data, Data[Comments], "\n\n"),
"Please select only one Country from the list."
)
However, I've now separated [Comments] from the Data dataset into another dataset called Comment. I'm not sure how the relationships quite work but I checked the Manage Relationships dialog box and a 1:M relationship is active between the Data and Comment datasets.
This is how both datasets look like:
> Data dataset
Month | Year | Region | Prod % | ...
Jan | 2016 | NAM | 80% | ...
Jan | 2016 | LAM | 40 | ...
Jan | 2016 | EUR | 60% | ...
Jan | 2016 | AFR | 70% | ...
Jan | 2016 | SEA | 80% | ...
Jan | 2016 | GCN | 70% | ...
> Comment dataset
Month | Year | Region | Comments
Jan | 2016 | NAM | Jan NAM comment here
Jan | 2016 | LAM | Jan LAM comment here
Jan | 2016 | EUR | Jan EUR comment here
Jan | 2016 | AFR | Jan AFR comment here
Jan | 2016 | SEA | Jan SEA comment here
Jan | 2016 | GCN | Jan GCN comment here
Is it possible that when the user selects a [Month], [Year] and [Region] from the Data dataset, it would display the appropriate [Comments] from the Comment dataset? Also, I'm not sure on how Primary Keys work in Power BI (both datasets don't have any Primary Keys).
Edit: So I tried this formula after merging the Comment dataset with the Data dataset:
Notes =
IF(
HASONEFILTER(Data[Region]) && HASONEFILTER(Data[Year]) && HASONEFILTER(Data[Month]),
CONCATENATEX(Data, Data[Comment.Comment], "\n\n"),
"Please select only one Period and Region from the list"
)
However, it is displaying the comment multiple times (there were 4 countries selected in the dataset. Would it be possible to make it display the [Comment] that is unique to that [Month], [Year] and [Region]?
I would add a VALUES function to your CONCATENATEX function to summarize the Comments, e.g.
CONCATENATEX( VALUES ( Data[Comment.Comment] ) , Data[Comment.Comment], "\n\n")