Forum Discussion

gl's avatar
gl
Advocate I
9 years ago
Solved

Setting a bar chart to a default selection

Hi All.   So, I’ve got a basic dataset containing a couple of columns along with a date column which spans back from 2011 to present.   I’m trying to create a bar graph which defaults to showing...
  • Anonymous's avatar
    Anonymous
    9 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.

     

    1. Do everything in my original solution except the Monthly Sales measure itself. I'm about to give a new formula for that. But first,
    2. 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.
    3. 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.
    4. 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.