Forum Discussion
Setting a bar chart to a default selection
- Anonymous9 years ago
gl If you're trying for my solution, you'll also need the third disconnected months table. I don't think Vvelarde's suggestion will work if you need to chart multiple months.
That formula has a check for IF(HASONEVALUE(DateTable[Month])... and on a chart with months on the x-axis, each month has one value for month, so it will always ignore that slicer rule on the chart.
Now, that being said my solution won't work on a chart with multiple months for the same reason. From your original description I thought you only ever wanted to show one month at a time: either last month by default, or a selected month in a slicer. But there's a way around it.
- Do everything in my original solution except the Monthly Sales measure itself. I'm about to give a new formula for that. But first,
- Create an inactive relationship between DateTable[Month of Year] and Months[Month of Year]. It has to be inactive because we only want to use it part of the time.
- Figure out what the maximum number of months you want to graph should be. Now that I've reread your original post I see that you wanted to select multiple months or even a whole year, rather than selecting a single month like I thought you wanted. In my example I'm setting the threshold at 24. That means you can see a graph with one month or multiple months, up to 2 full years. Any more than that and it will default back to last month only. I can't think of a good way around this limitation, sorry. You can set the threshold as large as you want but it can't be every month possible. Selecting every month is the same as selecting no months in the slicer, and we want no selection to default back to last month. So you just have to pick a number that works for your personal situation. My formula uses 24 because in my experience looking at more than 2 years worth of monthly data starts to look pretty messy, and you should probably set up a different measure and chart for that sort of thing.
- That's it. Except the new formula...
Monthly Sales = IF( HASONEVALUE(Months[Month of Year]), CALCULATE( SUM(MockSales[Amount]), USERELATIONSHIP( Months[Month of Year], DateTable[Month of Year] ) ), IF( COUNTROWS(Months) > 24, CALCULATE( SUM(MockSales[Amount]), FILTER( DateTable, DateTable[MonthDiff] = -1 ) ), CALCULATE( SUM(MockSales[Amount]), USERELATIONSHIP( Months[Month of Year], DateTable[Month of Year] ) ) ) )
If you need that translated back to English:
Monthly Sales = If I've selected 1 month in the Months slicer, then use the slicer selection to filter the DateTable,
else if I've selected 0 or more than 24 months in the Months slicer, then give me just last month's sales total,
else if I've selected more than 1 but less than or equal to 24 months in the Months Slicer, then use the slicer selection to filter the DateTable.
An alternative solution is:
a) A Date Table, with Date, year , month and other columns that you need.
b) Related To your table using Date column
c) use 2 slicers: Year and Month.
d) create a measure with this dax:
SalesTotal = if(HASONEVALUE(Calendario[Month]),CALCULATE(SUM(Sales[Sales])),CALCULATE(Sum(Sales[Sales]),DATEADD(LASTDATE(Sales[Date]),-1;MONTH)))
Thanks for your reply... however i'm having a bit of an issue when I try this. I've created the [Date Table] which contains the following fields
I've added the two slicers from this table, and also created the relationship between this and the main table. However when nothing is selected all of the months are still showing. I'm using the field 'MonthYear' from the above table for the axis.
This is the DAX measure i'm using...
Measure = if(HASONEVALUE('Date Table'[Month]),CALCULATE(Count('Total Calls'[Call Ref])),CALCULATE(Count('Total Calls'[Call Ref]),DATEADD(LASTDATE('Total Calls'[Date]),-1,MONTH)))
For reference, this is the contents of the 'Total Calls' table.
Any ideas?? I'm really pleased that there is a solution to this issue though as there are quite a few other graphs I can replicate this way too. And i've voted for a solution in the suggestions forum.
Thanks for all your help so far!
- Anonymous9 years agoNot applicable
gl If you're trying for my solution, you'll also need the third disconnected months table. I don't think Vvelarde's suggestion will work if you need to chart multiple months.
That formula has a check for IF(HASONEVALUE(DateTable[Month])... and on a chart with months on the x-axis, each month has one value for month, so it will always ignore that slicer rule on the chart.
Now, that being said my solution won't work on a chart with multiple months for the same reason. From your original description I thought you only ever wanted to show one month at a time: either last month by default, or a selected month in a slicer. But there's a way around it.
- Do everything in my original solution except the Monthly Sales measure itself. I'm about to give a new formula for that. But first,
- Create an inactive relationship between DateTable[Month of Year] and Months[Month of Year]. It has to be inactive because we only want to use it part of the time.
- Figure out what the maximum number of months you want to graph should be. Now that I've reread your original post I see that you wanted to select multiple months or even a whole year, rather than selecting a single month like I thought you wanted. In my example I'm setting the threshold at 24. That means you can see a graph with one month or multiple months, up to 2 full years. Any more than that and it will default back to last month only. I can't think of a good way around this limitation, sorry. You can set the threshold as large as you want but it can't be every month possible. Selecting every month is the same as selecting no months in the slicer, and we want no selection to default back to last month. So you just have to pick a number that works for your personal situation. My formula uses 24 because in my experience looking at more than 2 years worth of monthly data starts to look pretty messy, and you should probably set up a different measure and chart for that sort of thing.
- That's it. Except the new formula...
Monthly Sales = IF( HASONEVALUE(Months[Month of Year]), CALCULATE( SUM(MockSales[Amount]), USERELATIONSHIP( Months[Month of Year], DateTable[Month of Year] ) ), IF( COUNTROWS(Months) > 24, CALCULATE( SUM(MockSales[Amount]), FILTER( DateTable, DateTable[MonthDiff] = -1 ) ), CALCULATE( SUM(MockSales[Amount]), USERELATIONSHIP( Months[Month of Year], DateTable[Month of Year] ) ) ) )
If you need that translated back to English:
Monthly Sales = If I've selected 1 month in the Months slicer, then use the slicer selection to filter the DateTable,
else if I've selected 0 or more than 24 months in the Months slicer, then give me just last month's sales total,
else if I've selected more than 1 but less than or equal to 24 months in the Months Slicer, then use the slicer selection to filter the DateTable.- gl9 years agoAdvocate I
Anonymous Absolutely perfect! Thanks a lot for your help.