How to create a custom date period slicer - powerbi

Is it possible to have single selection radio buttons slicer that would filter by Today() - 7 days, Today() - 30 days and Today() - 60 days? So, to plot the last 7 days values, 30 days values or 60 days values. I'm able to single select by Quarters but instead of quarters use a range of days based on today's date? I tried filters but didn't get it to work.
Thank you for your help.

Add a calculated table:
Date Periods =
UNION(
ADDCOLUMNS(
CALENDAR(TODAY() - 7, TODAY()),
"Period", "Last 7 days",
"Sort", 1
),
ADDCOLUMNS(
CALENDAR(TODAY() - 30, TODAY()),
"Period", "Last 30 days",
"Sort", 2
),
ADDCOLUMNS(
CALENDAR(TODAY() - 60, TODAY()),
"Period", "Last 60 days",
"Sort", 3
)
)
Sort the Period column by the Sort column
Create a bi-directional relationship between 'Date Periods'[Date] and your days
Add 'Date Periods'[Period] to a slicer and set the selection settings to Single select
The result should look like this:

Related

PowerBI / DAX - REMOVEFILTERS not working as expected

We are using a date table based on a fiscal calendar. I need to determine if a particular year has 53 weeks or 52 weeks.
The page has a slicer with a date hierarchy that is used to select a year, quarter, month, or week. I would like the Measure to return the maximum number of weeks in a particular year regardless of what is selected.
The measure below works fine, it returns 53 which is the maximum number of weeks in any year regardless of the selection in the date hierarchy slicer.
PriorYearNumberOfWeeks = calculate(max(vwPBIDate[Week of Year]),REMOVEFILTERS(vwPBIDate))
However, when I try to add a filter condition to select the proper year it no longer works it returns the maximum week number of the period selected in the hierarchy. If I select January (a 5 week month) it returns 5 instead of 53 then number weeks in fiscal 2021.
PriorYearNumberOfWeeks = calculate(max(vwPBIDate[Week of Year]),REMOVEFILTERS(vwPBIDate),filter(vwPBIDate,vwPBIDate[Fiscal Year Number]=2021))
You're missing an ALL:
=
CALCULATE(
MAX( vwPBIDate[Week of Year] ),
REMOVEFILTERS( vwPBIDate ),
FILTER(
ALL( vwPBIDate ),
vwPBIDate[Fiscal Year Number] = 2021
)
)
which is equivalent to:
=
CALCULATE(
MAX( vwPBIDate[Week of Year] ),
REMOVEFILTERS( vwPBIDate ),
vwPBIDate[Fiscal Year Number] = 2021
)

Calculate age cluster dynamically in DAX - Power BI

I have created a report for the HR department. One of the visuals aims to display the pyramid of ages of the employees by gender (we take cluster of 5 years, e.g. 20-25 for people between 20 and 25 years old).
To make it simple, data wise, I have a table with the list of employees, including their date of birth and many other fields, not relevant for this post. I added a calculated column with the age cluster based on the today’s date:
AgeCluster =
VAR AgeCalc=if(HR_DATA[Birthdate]=blank(),blank(),DATEDIFF(HR_DATA[Birthdate],today(),YEAR))
VAR Mult5=INT(AgeCalc/5)
RETURN
if(isblank(AgeCalc),blank(),5*Mult5&"-"&5*(Mult5+1))
And I have a basic visual (tornado chart with the AgeCluster in Group, showing male and female)
Now my issue is that my report should be dynamic, so the user should be able to see the situation in the past or in the future... I have a calendar table (not linked to my HR_Data table), and a date slicer on my report's page. I need the age cluster to be recalculated.
I have tried a calculated table, but I can’t get it working properly. I have read various blog posts on similar issue, but still can't figure out how to solve it...
Any idea or tips much appreciated.
Thank you so much!
Dynamic filtering usually means in these situations that you need a fixed x-axis to work with. That x-axis is calculated outside the original table, such as a parameter table.
Assuming your data looks like this:
Table: Employees
EmployeeID
DOB
1
20 March 1977
2
05 December 1981
3
25 December 1951
4
20 December 1954
5
04 March 1980
6
24 July 1968
7
07 June 1984
8
01 October 1992
9
25 February 1999
10
02 November 1987
First, we need to create the Age Groups with their respective lower and top bands. This table uses a similar code when a parameter table is created.
Table: Buckets
Because you are using whole numbers, I think is best to calculate the TopBand by adding 4. In that case, you don't have repeating numbers. In other words, ranges shouldn't overlap (20-25 and 25-30)
Buckets =
SELECTCOLUMNS (
GENERATESERIES ( 20, 80, 5 ),
"Age Series",
[Value] & " - " & [Value] + 4,
"LowerBand", [Value],
"TopBand", [Value] + 4
)
Also, we will need a Calendar Table to filter accordingly.
Table: CalendarHR
CalendarHR =
ADDCOLUMNS (
CALENDAR ( MIN ( Employees[DOB] ), TODAY () ),
"Year", YEAR ( [Date] )
)
With all that, you can create a calculation that can be filter dynamically.
DAX Measure:
EmployeesbyBracket =
VAR _SelectedDate =
MAX ( CalendarHR[Date] )
VAR _SelectedLowerBand =
SELECTEDVALUE ( Buckets[LowerBand] )
VAR _SelectedTopBand =
SELECTEDVALUE ( Buckets[TopBand] )
VAR EmployeesAge =
ADDCOLUMNS (
Employees,
"Age",
INT ( DATEDIFF ( [DOB], _SelectedDate, YEAR ) / 5 ) * 5
)
RETURN
COUNTROWS (
FILTER (
EmployeesAge,
[Age] >= _SelectedLowerBand
&& [Age] <= _SelectedTopBand
)
)
Output
I've created a table visual with the Age Groups from the Buckets table.
Both a calculated column and a calculated table will be static and not achieve your desired objective. You need age to be calculated via a measure.
Create a disconnected table with your age buckets and then implement the dynamic segmentation pattern.

DAX Dynamic Date Averages

I am trying to create a single measure that will calculate a date average, depending on the drill down on a time barchart.
The bar chart has 5 time pieces in the hierarchy. Year - Quarter - Month - Week -Day
As I drill through the bar chart, I want a measure that will dynamically calculate the Yearly Avg - Quarterly Avg - Monthly Avg, Weekly Avg, Daily Avg.
For example, lets say the barchart is on the Year Level and displaying 4 bars representing 2016-2019. The vale of the measure would be COUNT(UnitID) / 4 because there are 4 bars currently displayed on the X axis.
Lets drill into 2018 to the month level. There are 12 bars representing Jan-Dec. The value of the measure would be COUNT(2018 UnitIDs) / 12 because there are 12 bars currently displayed on the X axis.
Lets drill into 2019 to the month level. There are only 10 bars representing Jan-Oct. The value of the measure would be COUNT(2019 UnitIDs) / 10 because there are only 10 bars currently displayed on the X axis.
Finally, lets not drill down, but just expand the hierarchy from year to month. We go from the yearly view showing 4 bars to the monthly view showing 12. But the Jan total is the sum of 2016Jan + 2017Jan + 2018Jan + 2019Jan. The measure needs to interpret this as COUNT(All UnitIDs) / 12 because there are 12 months currently displayed on the X Axis.
I'm basically trying to figure out how to create a measure that counts whatever number of values are on the X axis at any given time.
Thank you all for your advice and feedback. I'm really looking forward to testing your responses and seeing if they work!
It sounds like you are looking for a way to show the same value for each year, quarter, month, etc.
I wonder how this would make sense as a visual, but if that is the case, your measure would be something like this.
Average Count UnitIDs in Drilldown Periods =
-- At which level the report is drilled down?
VAR CalendarDrilldownLevel =
IF(ISFILTERED('Calendar'[Day]), "Day",
IF(ISFILTERED('Calendar'[Week]), "Week",
IF(ISFILTERED('Calendar'[Month]), "Month",
IF(ISFILTERED('Calendar'[Quarter]), "Quarter",
IF(ISFILTERED('Calendar'[Year]), "Year", "None")))))
-- Total count of UnitIDs in the entire period
VAR TotalCount = CALCULATE([Count of Unit IDs], ALLSELECTED('Calendar'))
-- Number of years, quarters, months, etc. based on the drilldown level
VAR CountOfPeriods = CALCULATE(
SWITCH(
CalendarDrilldownLevel,
"Year", DISTINCTCOUNT('Calendar'[Year]),
"Quarter", COUNTROWS(
DISTINCT(
SELECTCOLUMNS(
'Calendar',
"Year", 'Calendar'[Year],
"Quarter", 'Calendar'[Quarter]
)
)
),
"Month", COUNTROWS(
DISTINCT(
SELECTCOLUMNS(
'Calendar',
"Year", 'Calendar'[Year],
"Quarter", 'Calendar'[Quarter],
"Month", 'Calendar'[Month]
)
)
),
-- Similar lines follow for weeks, days, and in case of no drill down.
),
ALLSELECTED('Calendar')
)
RETURN DIVIDE(TotalCount, CountOfPeriods)
Maybe you are trying this to show average line in the visual? In that case, you can just define a simple measure like COUNT('Your Table'[UnitID]) and add an average line in column chart visual setting. (You can find that in Analytics section)

Find Maximum month from two slicers

I have this data in a pbix file:
ImaginaryData =
DATATABLE (
"Month", DATETIME,
"Amount", INTEGER,
{
{ "01 JAN 2018", 10 },
{ "01 FEB 2018", 15 },
{ "01 MAR 2018", 25 },
{ "01 APR 2018", 60 },
{ "01 MAY 2018", 55 }
}
On the canvas we have two Date field slicers:
How do I create two measures?:
Max Month Selected
Min Month Selected
I tried this for Max Month Selected but it does not work:
Max Month Selected =
Calculate(
MAX(ImaginaryData[Month]),
ImaginaryData
)
When two or more slicers filter the same column, you will always get the intersection of values - not the union. So in other words, if you select one value in the first slicer, and a different value in the second slicer, you would basically end up with a blank filter. That can also be seen if you drag the [Amount] column into your report:
Instead, you should use a single slicer and allowing the user to select a range of months, for example using the "Between" option:
Then, the following measures should work as expected:
Max Month Selected = MAX(ImaginaryData[Month])
Min Month Selected = MIN(ImaginaryData[Month])
If you must have two dropdown slicers, the only workaround is to create two tables, both containing the [Month] column, and have one slicer pointing to one table, and the other slicer pointing to the second table. Then, you would define your measures as:
Max Month Selected = MAX(MAX('MonthTable1'[Month]), MAX('MonthTable2'[Month]))
Min Month Selected = MIN(MIN('MonthTable1'[Month]), MIN('MonthTable2'[Month]))

Rolling 3 months DAX

I have had a headache since yesterday with a measure.
My table is Data and in this table, I have a column Data[Date] which contains the first day of each month from January to June and a measure which calculates Total revenue.
I need a measure that calculates for June: Total REV = April + May + June.
I have tried this measure:
CALCULATE([TOTAL REV], DATESINPERIOD(Data[Date], LASTDATE(Data[Date]), -3, MONTH))
but in order to have the correct number I need to select all three months in the slicer, not just the month I am interested in.
When you've got just one date selected in the slicer the filter context for the CALCULATE is just that month.
So I suspect that something similar to the below would fix your issue as it would allow the CALCULATE to consider the whole date table:
CALCULATE([TOTAL REV], DATESINPERIOD( ALL(Data[Date]), LASTDATE(Data[Date]), -3, MONTH))
Try this:
sumx(DATESINPERIOD('Data'[Date], LASTDATE('Data'[Date]), -3, MONTH) , [TOTAL REV])