Forum Discussion
Showing data weekly or monthly dynamically
- 2 years ago
Hi there Anonymous
Now that field parameters are available, I would recommend using that feature as a means of changing the field used on the axis.
https://learn.microsoft.com/en-us/power-bi/create-reports/power-bi-field-parameters
Using field parameters avoids the modelling used in the post you linked to (wow, that was 7 years ago!).
Please post back if more guidance needed.
Regards
It seems that "Field Parameters" feature is very new in PowerBI and unfortunately my Power BI Desktop RS (january-2023) does not support yet! Hmm
Ah, I see!
It turns out that the Field Parameters feature is still in preview so it won't be in Report Server versions yet.
I have attached a PBIX with an example using a modelling approach instead.
1. Create a Date Grouping table that looks something like this.
It contains every date from the 'Date' table grouped into both Weeks and Months.
Date Grouping is either "Weekly" or "Monthly".
Label is the label that will appear in the visual, and has to be of type text to handle different grouping types.
Label Sort is constructed to give the appropriate sort order for each of Weekly/Monthly labels, but must have a 1:1 mapping with Label.
Date contains the dates corresponding to each Label.
This can be generated with Power Query or DAX. I used DAX in my example:
Date Grouping =
VAR Monthly =
SELECTCOLUMNS (
SUMMARIZE ( 'Date', 'Date'[Start of Month], 'Date'[Date] ),
"Date Grouping", "Monthly",
"Label", FORMAT ( 'Date'[Start of Month], "mmm-yyyy" ),
"Label Sort Original", 'Date'[Start of Month],
"Date", 'Date'[Date]
)
VAR Weekly =
SELECTCOLUMNS (
SUMMARIZE (
'Date',
'Date'[Fiscal Year Week],
'Date'[Fiscal Year Week Number],
'Date'[Date]
),
"Date Grouping", "Weekly",
"Label", 'Date'[Fiscal Year Week],
"Label Sort Original", 'Date'[Fiscal Year Week Number],
"Date", 'Date'[Date]
)
VAR Combined =
UNION ( Monthly, Weekly )
VAR Result =
SELECTCOLUMNS (
Combined,
"Date Grouping", [Date Grouping],
"Label", [Label],
"Label Sort",
RANK (
DENSE,
Combined,
ORDERBY ( [Date Grouping], ASC, [Label Sort Original], ASC )
),
"Date", [Date]
)
RETURN
Result
2. Create a 1:many bidirectional relationship between 'Date'[Date] and 'Date Grouping'[Date].
3. Create a single-selection slicer using 'Date Grouping'[Date Grouping].
3. Place 'Date Grouping'[Label] on the axis of the visual.
Hopefully you can adapt this to your particular model 🙂
Regards