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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
paweldm
Helper II
Helper II

% change of open events

Hi ,

 

I am calculating events in progress based on the startDate and closeDate over year.

I use 2 tables.

table1 includes: pharmaId | startDate | closeDate

table2 is a date table and it includs date | year | month | etc. 

Tabel 2 is not conected with table 1. 

 

The measure to calculate open events works fine.

 

Pharmacy Active = CALCULATE(
	COUNT('pharmacy'[pharmaid]);
		FILTER(
				'pharmacy';
				'pharmacy'[opendate] <= LASTDATE('Date'[Date]) &&
				('pharmacy'[closeDate] >= FIRSTDATE('Date'[Date]) || ISBLANK('pharmacy'[closeDate]))
			)
)

 

I need to calculate the % change of open pharmacies over time.

 

For example, I would like to select 2 years from the date table in the slicer: 2010 and 2015 and calculate the measure to find out the % change of open pharmacies from 2010 to 2015.

 

However, I would like to select dynamically other years as well and switch to another period % change calculation. 

 

In other words, I would like to apply the % change measure for my calculation of events-in-progress.

 

I have found the tutorial on dynamic % change measure, but I don't know how to modify it to my case of open events.

https://powerbi.tips/2016/06/dynamic-percent-change-using-dax/  

 

Can anyone help me to deal with that?

 

Pawel

 

1 ACCEPTED SOLUTION
Vvelarde
Community Champion
Community Champion

@paweldm

 

Hi, i give you a idea:

 

Using a Slicer With Years (From Calendar Table).

 

3 Measures

 

 

PharmacyActiveFirstYearSelected =
VAR FirstyearSelected =
    FIRSTNONBLANK ( VALUES ( 'Calendar'[Year] ), 'Calendar'[Year] )
VAR LastDay =
    CALCULATE (
        LASTDATE ( 'Calendar'[Date] ),
        FILTER ( 'Calendar', 'Calendar'[Year] = FirstyearSelected )
    )
RETURN
    CALCULATE (
        COUNTROWS ( Pharmacy ),
        FILTER (
            ALL ( Pharmacy ),
            Pharmacy[Start Date] <= lastday
                && (
                    Pharmacy[Close Data] >= lastday
                        || Pharmacy[Close Data] = BLANK ()
                )
        )
    )

 

 

PharmacyActiveLastYear =
VAR LastyearSelected =
    LASTNONBLANK ( VALUES ( 'Calendar'[Year] ), 'Calendar'[Year] )
VAR LastDay =
    CALCULATE (
        LASTDATE ( 'Calendar'[Date] ),
        FILTER ( 'Calendar', 'Calendar'[Year] = LastyearSelected )
    )
RETURN
    CALCULATE (
        COUNTROWS ( Pharmacy ),
        FILTER (
            ALL ( Pharmacy ),
            Pharmacy[Start Date] <= lastday
                && (
                    Pharmacy[Close Data] >= lastday
                        || Pharmacy[Close Data] = BLANK ()
                )
        )
    )

 

 

%Change =
DIVIDE ( [PharmacyActiveLastYear], [PharmacyActiveFirstYear] ) - 1

 




Lima - Peru

View solution in original post

3 REPLIES 3
paweldm
Helper II
Helper II

I belive I am now a bit closer to the solution.

 

I built the table with SUMMARIZE function:

 

 

SummarizeActive =
SUMMARIZE ( 'Date'; 'Date'[Year]; "Total Active By Year"; [Pharmacy Active] )

 

Next I followed the manual from https://powerbi.tips/2016/06/dynamic-percent-change-using-dax/  and got the % change with 'Year' slicer from newly created 'SummarizeActive' tabel.

 

However, I need to be able to add to the slicer [Year] column from the 'Date' table to dynamically change the stacked column chart with the measure of [Pharmacy Active] and in the same time dynamically calculate % change. 

 

Vvelarde
Community Champion
Community Champion

@paweldm

 

Hi, i give you a idea:

 

Using a Slicer With Years (From Calendar Table).

 

3 Measures

 

 

PharmacyActiveFirstYearSelected =
VAR FirstyearSelected =
    FIRSTNONBLANK ( VALUES ( 'Calendar'[Year] ), 'Calendar'[Year] )
VAR LastDay =
    CALCULATE (
        LASTDATE ( 'Calendar'[Date] ),
        FILTER ( 'Calendar', 'Calendar'[Year] = FirstyearSelected )
    )
RETURN
    CALCULATE (
        COUNTROWS ( Pharmacy ),
        FILTER (
            ALL ( Pharmacy ),
            Pharmacy[Start Date] <= lastday
                && (
                    Pharmacy[Close Data] >= lastday
                        || Pharmacy[Close Data] = BLANK ()
                )
        )
    )

 

 

PharmacyActiveLastYear =
VAR LastyearSelected =
    LASTNONBLANK ( VALUES ( 'Calendar'[Year] ), 'Calendar'[Year] )
VAR LastDay =
    CALCULATE (
        LASTDATE ( 'Calendar'[Date] ),
        FILTER ( 'Calendar', 'Calendar'[Year] = LastyearSelected )
    )
RETURN
    CALCULATE (
        COUNTROWS ( Pharmacy ),
        FILTER (
            ALL ( Pharmacy ),
            Pharmacy[Start Date] <= lastday
                && (
                    Pharmacy[Close Data] >= lastday
                        || Pharmacy[Close Data] = BLANK ()
                )
        )
    )

 

 

%Change =
DIVIDE ( [PharmacyActiveLastYear], [PharmacyActiveFirstYear] ) - 1

 




Lima - Peru

@Vvelarde

 

Hi Victor!

 

Your measures work excellent. It is the solution I needed.

Thank you so much. I am so impressed by your DAX skills. Power On!

p.s. I have only changed the "ALL" function into "ALLSELECTED" in the 13th line to use other slicers in my data model. 

 

Pawel

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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