简体   繁体   中英

How to display data in area chart starting from the year chosen on a slicer and get all following years in Power BI

When choose a single Year on slicer I want area chart display all data from that chosen year and till the end (all years I have in my datasource). But instead it just displays me the data for single year choosing on a slicer. So I have this:

在此处输入图片说明

But I want it look like this: whatever Year I choose in slicer - chart will show all data starting from 2014 and goes till 2017.

在此处输入图片说明

I am simply following a PowerBI template example and it seems like it's possible to do that:

https://app.powerbi.com/view?r=eyJrIjoiMjc2NzExODItMjNhYy00ZWMxLWI2NGItYjFiNWMzYzUzMzhlIiwidCI6IjU3NGMzZTU2LTQ5MjQtNDAwNC1hZDFhLWQ4NDI3ZTdkYjI0MSIsImMiOjZ9

This is doable but it requires some tricks and extra measures.

TL;DR: The slicer you see is actually served as a value picker , not as a filter. An extra measure based on the value is created and used as visual level filter for the visual to do the trick.


If you want to follow along, you can download the .pbix file from this Microsoft edX course about Power BI .

First, create a new table based on the existing Date table, with only distinct years:

Year = DISTINCT('Date'[Year])

年


Then, create a slicer with the Year column from the newly created Year table ( NOT the Date table).

切片机


A measure (used as flag) is created as follows:

Flag = 
VAR YearSelected = FIRSTNONBLANK(VALUES('Year'[Year]), 0)
RETURN
IF(VALUES('Date'[Year]) >= YearSelected, 1, 0)

So basically it gets the year selected from the year slicer and compare it with the year value in the date table to see if it's greater than or equal to it.


The chart is created with Year column from the Date table ( NOT the Year table), and other needed measures. Flag is added to the Visual level filters and set to 1.

图表

So the Flag value will change according to the value picked in the Year slicer, and served as the actual filter to the chart displayed.


Results:

结果


EDIT: on more use cases

@Oleg Try to think of how you can apply the Flag concept further. For example, if you want another chart displaying data of the same year as the slicer, you can set up another flag called SameYearFlag and only change the part of value comparison to = . Add it to the chart Visual level filter and it'll show only data in the same year. Yes, by extension, that means you can have another flags like LastYearFlag , NextYearFlag , etc, as long as it makes sense to you. The use case is up to you.

LastYearFlag = 
VAR YearSelected = FIRSTNONBLANK(VALUES('Year'[Year]), 0)
RETURN
IF(YearSelected - VALUES('Date'[Year]) = 1, 1, 0)

NextYearFlag = 
VAR YearSelected = FIRSTNONBLANK(VALUES('Year'[Year]), 0)
RETURN
IF(VALUES('Date'[Year]) - YearSelected = 1, 1, 0)

SameYearFlag = 
VAR YearSelected = FIRSTNONBLANK(VALUES('Year'[Year]), 0)
RETURN
IF(VALUES('Date'[Year]) = YearSelected, 1, 0)

Examples:

多个图表

By having only one year slicer, I can have charts with data in the same year, last year, next year and all years following, by applying different flags to them.

As said, it's up to you to come up with more interesting use cases!

I would propose to consider the new numeric range slicer. You can just set it as "Greater than or equal to". Users can select then the initial year in the range by entering the number or dragging the slicer.

You would need to enable this feature in Power Bi Desktop, Options under "Preview features".

Is well presented in the documentation https://powerbi.microsoft.com/en-us/documentation/powerbi-desktop-slicer-numeric-range/

This is how it could look like:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM