Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
Bendelyon
Helper I
Helper I

Change the granularity of the x axis

Hi everyones, 

 

I have a slicer of dates whitch allow me to choose the dates between i gonna visualize my chart graph.

Currently i show my data per months.

 

I want that my chart graph become dynamic and the conditions gonna be thoses ones:

- If the dates choosen are minus one month i want to see my data per weeks

- If the dates choosen are minus one week i whant to see my data per days

- Else i want to see my data per month

 

I thing that DAX measures can help me to find a solution but i do not know how i can made it.

 

If someone has a solution.

 

Thank you

 

1 ACCEPTED SOLUTION
johnt75
Super User
Super User

You can adapt the basic technique described in https://www.youtube.com/watch?v=KReYWx5NXYg but instead make it dynamic.

Create a field parameter containing the different granularities that you want, in your case days, weeks and months. Edit the code of the field parameter table to add 2 new columns, which will specify the min and max number of days which you want to show at that granularity. Use BLANK() for the largest granularity's max. Rename the new columns to [Min Days] and [Max Days]. You will have a table something like

Date Precision.PNG

You can obviously use whatever numbers you like for the min and max for each granularity, but make sure that the max of one granularity is the same as the min for the next one.

Create a measure like

Choose Date Precision = VAR FirstVisibleDate = MIN( 'Date'[Date] )
VAR LastVisibleDate = MAX( 'Date'[Date] )
VAR DaysDiff = DATEDIFF( FirstVisibleDate, LastVisibleDate, DAY)
VAR MinDays = SELECTEDVALUE( 'Date Precision'[Min Days] )
VAR MaxDays = SELECTEDVALUE( 'Date Precision'[Max Days] )
VAR Result = IF( MinDays <= DaysDiff && ( ISBLANK( MaxDays ) || MaxDays > DaysDiff ), 1, 0 )

RETURN Result

Add the field parameter to the visual, and then add the field parameter as a filter on the visual. Set the filter to be TopN, use 1 as the number of items to show, and use the measure as the value.

Now the visual will change granularity based on the number of visible dates

View solution in original post

2 REPLIES 2
johnt75
Super User
Super User

You can adapt the basic technique described in https://www.youtube.com/watch?v=KReYWx5NXYg but instead make it dynamic.

Create a field parameter containing the different granularities that you want, in your case days, weeks and months. Edit the code of the field parameter table to add 2 new columns, which will specify the min and max number of days which you want to show at that granularity. Use BLANK() for the largest granularity's max. Rename the new columns to [Min Days] and [Max Days]. You will have a table something like

Date Precision.PNG

You can obviously use whatever numbers you like for the min and max for each granularity, but make sure that the max of one granularity is the same as the min for the next one.

Create a measure like

Choose Date Precision = VAR FirstVisibleDate = MIN( 'Date'[Date] )
VAR LastVisibleDate = MAX( 'Date'[Date] )
VAR DaysDiff = DATEDIFF( FirstVisibleDate, LastVisibleDate, DAY)
VAR MinDays = SELECTEDVALUE( 'Date Precision'[Min Days] )
VAR MaxDays = SELECTEDVALUE( 'Date Precision'[Max Days] )
VAR Result = IF( MinDays <= DaysDiff && ( ISBLANK( MaxDays ) || MaxDays > DaysDiff ), 1, 0 )

RETURN Result

Add the field parameter to the visual, and then add the field parameter as a filter on the visual. Set the filter to be TopN, use 1 as the number of items to show, and use the measure as the value.

Now the visual will change granularity based on the number of visible dates

lbendlin
Super User
Super User

Throw away your hierarchy.  Add the Date column to the X axis by itself.  Set the axis type to Continuous. Enable Zoom Sliders.  The visual will do all the rest of the work for you.

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

Find out what's new and trending in the Fabric Community.

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

Check out the November 2024 Power BI update to learn about new features.