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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
chamue329
Helper II
Helper II

Daily change rate with multiple customers

Hi,

 

I have a table with customer sales by date and I need to calculate the daily change and % change (example image below). Is it better to add these two columns in Power Query or create DAX measures?  I've been scouring the message boards but can't seem to find a solution for this scenario. 

 

CustomerDateRunning SalesDaily Chg% Daily Chg
A3/2/202010--
A3/3/20202515150%
A3/4/2020401560%
A3/5/20209757143%
B3/2/2020100--
B3/3/20201202020%
B3/4/20201907058%
B3/5/20202203016%
1 ACCEPTED SOLUTION
az38
Community Champion
Community Champion

@chamue329 

DAX is appropriate solution foryour task.

try create a measures

Daily Chg = 
var _prevDate = CALCULATE(MAX('Table'[Date]),FILTER(ALL('Table'),'Table'[Customer]=SELECTEDVALUE('Table'[Customer]) && 'Table'[Date]<SELECTEDVALUE('Table'[Date])))
RETURN
IF(ISBLANK(_prevDate), BLANK(),
SELECTEDVALUE('Table'[Running Sales])-CALCULATE(MAX('Table'[Running Sales]),FILTER(ALL('Table'),'Table'[Customer]=SELECTEDVALUE('Table'[Customer]) && 'Table'[Date]=_prevDate)))

and

% Daily Chg = 
var _prevDate = CALCULATE(MAX('Table'[Date]),FILTER(ALL('Table'),'Table'[Customer]=SELECTEDVALUE('Table'[Customer]) && 'Table'[Date]<SELECTEDVALUE('Table'[Date])))
RETURN
IF(ISBLANK(_prevDate), BLANK(),
DIVIDE(SELECTEDVALUE('Table'[Running Sales]), CALCULATE(MAX('Table'[Running Sales]),FILTER(ALL('Table'),'Table'[Customer]=SELECTEDVALUE('Table'[Customer]) && 'Table'[Date]=_prevDate))) - 1)

do not hesitate to give a kudo to useful posts and mark solutions as solution
LinkedIn

View solution in original post

6 REPLIES 6
Greg_Deckler
Super User
Super User

This should be basically the same as MTBF - See my article on Mean Time Between Failure (MTBF) which uses EARLIER: http://community.powerbi.com/t5/Community-Blog/Mean-Time-Between-Failure-MTBF-and-Power-BI/ba-p/3395...

 

In your case:

 

Daily Chg =
  VAR __Current = [Running Sales]
  VAR __PreviousDate = 
    MAXX(
      FILTER(
        'Table',
        [Customer] = EARLIER([Customer]) &&
        [Date] < EARLIER([Date])
      ),
      [Date]
    )
  VAR __Previous = 
     MAXX(FILTER('Table',[Customer = EARLIER([Customer]) && [Date] = __PreviousDate),[RunningSales])
RETURN
  __Current - __Previous

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
az38
Community Champion
Community Champion

@chamue329 

DAX is appropriate solution foryour task.

try create a measures

Daily Chg = 
var _prevDate = CALCULATE(MAX('Table'[Date]),FILTER(ALL('Table'),'Table'[Customer]=SELECTEDVALUE('Table'[Customer]) && 'Table'[Date]<SELECTEDVALUE('Table'[Date])))
RETURN
IF(ISBLANK(_prevDate), BLANK(),
SELECTEDVALUE('Table'[Running Sales])-CALCULATE(MAX('Table'[Running Sales]),FILTER(ALL('Table'),'Table'[Customer]=SELECTEDVALUE('Table'[Customer]) && 'Table'[Date]=_prevDate)))

and

% Daily Chg = 
var _prevDate = CALCULATE(MAX('Table'[Date]),FILTER(ALL('Table'),'Table'[Customer]=SELECTEDVALUE('Table'[Customer]) && 'Table'[Date]<SELECTEDVALUE('Table'[Date])))
RETURN
IF(ISBLANK(_prevDate), BLANK(),
DIVIDE(SELECTEDVALUE('Table'[Running Sales]), CALCULATE(MAX('Table'[Running Sales]),FILTER(ALL('Table'),'Table'[Customer]=SELECTEDVALUE('Table'[Customer]) && 'Table'[Date]=_prevDate))) - 1)

do not hesitate to give a kudo to useful posts and mark solutions as solution
LinkedIn

@az38 This works perfectly.  I was able to trend this out on a line chart and both measures display.  I have a follow-up question, in my table structure there is also a State column. When I add a second filter to my line chart to view it by State, it won't render. Is this simply some chart settings in Power BI to render the visual properly or am I stuck having to create a second set of measures if I wanted to generate for the aggregate of customers by State?  Here's my table field with State included:

 

StateCustomerDateRunning SalesDaily Chg% Daily Chg
CAA3/2/202010--
CAA3/3/20202515150%
CAA3/4/2020401560%
CAA3/5/20209757143%
CAB3/2/2020100--
CAB3/3/20201202020%
CAB3/4/20201907058%
CAB3/5/20202203016%

Perhaps a change in the DAX syntax to dynamically adjust to the filter selection (State or Customer) if that is feasible?

az38
Community Champion
Community Champion

@chamue329 

in my solution it would be like 

Daily Chg = 
var _prevDate = CALCULATE(MAX('Table'[Date]), FILTER(ALLEXCEPT('Table', 'Table'[Customer],'Table'[State]), 'Table'[Date] < SELECTEDVALUE('Table'[Date])))
RETURN
IF(ISBLANK(_prevDate), BLANK(),
SELECTEDVALUE('Table'[Running Sales]) - CALCULATE(MAX('Table'[Running Sales]), FILTER(ALLEXCEPT('Table', 'Table'[Customer], 'Table'[State]),  'Table'[Date]=_prevDate)))

do not hesitate to give a kudo to useful posts and mark solutions as solution
LinkedIn

@az38  Thanks for the modification - however I'm getting all negative numbers when I filter to States.  It works fine when I have the Customer column included, but once I remove and only look at my table by State, it gets messed up.  One clarification - I don't have rows in my tables that aggregate the State records.  I placed them in my example table for illustration.  I don't know if this will make a difference in your syntax.

 

Running Sales with daily changeRunning Sales with daily change

Helpful resources

Announcements
PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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