cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
DusanVH
Frequent Visitor

Problem creating a line graph with running sums and a simple projection

This is a follow-up from a previous post I made.

 Firstly I'll leave some example data:

 

package_IDclient_IDpackage_weightdue_datearrival_date
112003-03-202105-03-2021
211508-04-202102-04-2021
321808-03-202112-03-2021
432305-03-202103-03-2021
513520-05-2021 
631210-04-202131-03-2021
72813-04-202120-04-2021
833308-06-2021 
912610-06-2021 
1021409-06-2021 
1126008-07-2021 
1233215-07-2021 
131420-07-2021 

 

Just in case, dates are D-M-Y. The example data assumes a current date of 25-04-2021. Some dates in the arrival_date column are intentionally blank, since those are due in the future. Those are filled in as the packages arrive.

 

I also have a date table that's joined by both due and arrival dates.

 

What I need to achieve is a line graph that has three elements:

 

1) Running total for package_weight by due_date.
2) Running total for package_weight by arrival_date.
3) A simple projection for total package_weight based on a simple estimate. Something like this: first calculating a "daily weight" for last "interval", ie. last package_weight divided by days between penultimate arrival_date and last arrival_date, and then multiplied by amount of days until a given date, eg. 31-07-2021. That should give a very simple projection for the period from last arrival_date to 31-07-2021.

 

All of this per client. This is, I need to be able to use a slicer to view the data corresponding to a single client.

 

An example graph of what I want to achieve here:

 

DusanVH_0-1627681156429.png

 

In the graph blue is running total of package_weight by due_date, orange is running total of package_weightby arrival_date, and red is the projection.

 

In the previous post I mentioned I received a suggestion that helped me to make one of the running sums work, but not both at the same time in the same graph.

Here's my code:

Cumm due = CALCULATE(CALCULATE(SUM(Table[package_weight]), USERELATIONSHIP(date[date], table[due_date])), FILTER(ALLSELECTED(table[due_date]), ISONORAFTER(table[due_date], MAX(table[due_date]), DESC)))
And it doesn't work for cumm arrival:
Cumm arrival = CALCULATE(CALCULATE(SUM(Table[package_weight]), USERELATIONSHIP(date[date], table[arrival_date]), FILTER(ALLSELECTED(table[arrival_date]), ISONORAFTER(table[arrival_date], MAX(table[arrival_date]), DESC)))
Here's an example graph with different data but same logic of what I see:
DusanVH_1-1627681451144.png

 

Blue is Cumm due and cyan is Cumm arrival. Only Cumm due seems to work.

This is also using due_date as the graph's x axis (Axis box). When I put arrival_date there, it's reversed, Cumm arrival works and Cumm due doesn't. It's kinda obvious it shouldn't work, as I should use the date[date] instead, but when I do, the graph is empty. I imagine this is due to me not using the dates table in my code.

 

And I'm also missing the projection, and here apparently the forecast functionality is of no help in this case because the dates are not at regular intervals.

Any help is appreciated, thanks in advance!

2 REPLIES 2
v-jingzhang
Community Support
Community Support

Hi @DusanVH 

 

I guess the relationship between date[date] and table[arrival_date] is an active relationship in your model, so the code for cumm arrival doesn't work. Please remove the USERELATIONSHIP(date[date], table[arrival_date]) part from the code. USERELATIONSHIP is used to activate an inactive relationship in the code. For active relatioships, you don't need it. 

 

Another reason is that arrival_date column has blank values for future dates. However, blank values are considered less than other non-blank values, so package_weight on those rows will be calculated incorrectly. We need to filter out blank dates in the code. 

 

Below are my code

Cumm arrival = CALCULATE(SUM('Table'[package_weight]),FILTER(ALLSELECTED('Date'),NOT(ISBLANK('Date'[Date])) && 'Date'[Date]<=MAX('Date'[Date])))
Cumm due = CALCULATE(SUM('Table'[package_weight]), USERELATIONSHIP('Date'[Date], 'Table'[due_date]), FILTER(ALLSELECTED('Date'[Date]), 'Date'[Date]<=MAX('Date'[Date])))

 

And you should use 'date'[date] in both measures and on X Axis in the line chart. This will avoid missing any date value. 

080203.jpg

 

For the projection part, do you mean to divide the package_weight on the last arrival_date by days between penultimate arrival_date and last arrival_date? And how to get the ending given date? It's a fixed date or a dynamic date?

 

Regards,
Community Support Team _ Jing
If this post helps, please Accept it as the solution to help other members find it.

Hi @v-jingzhang , thanks for your suggestion, I'll try what you say, but I think you missed that it's per package. I think yours is adding up all packages, maybe you weren't using a slicer to select a package, I guess that should need no code modification. Also, your graphs are stepped, ideally they should be straight lines between each point.


For the projection part, do you mean to divide the package_weight on the last arrival_date by days between penultimate arrival_date and last arrival_date? And how to get the ending given date? It's a fixed date or a dynamic date?


  1. Yes, that's it. In other words, I want the staright line betweem the penultimate package point and the last one to be projected forwards.

  2. Ideally I'd like the projection to have a point at each of the next due_dates. If that's too difficult, using only the last due_date is fine. If even that is too difficult, something simple like last arrival_date + 3 months will do well enough. The projection should be updated as packages arrive.

    To illustrate this better I'll make some graphs. Again, blue is for due_date, orange is for arrival_date and red is for the projection. For each one assume current date to be one day after the last arrival_date. Also, there is a period_start date in another table that I now realized should be used as the starting "zero" point, which I used here to do the first projection.

    This first one is just after package #1 arrives:
    DusanVH_0-1628021723906.png

    package_IDclient_IDpackage_weightdue_date arr_daterunning_sum_whtProjectionProj_date
    01001-03-202101-03-20210  
    111030-03-202115-03-2021101015-03-2021
    212015-04-2021 303215-04-2021
    313002-05-2021 604402-05-2021
    414030-06-2021 1008630-06-2021


    Second one is just after package #2 arrives. In this one the projection exceeds the maximum of 100, that's intended:
    DusanVH_0-1628022593237.png

    package_noclient_IDpackage_weightdue_date arr_daterunning_sum_whtProjectionProj_date
    01001-03-202101-03-20210  
    111030-03-202115-03-202110  
    212015-04-202103-04-2021303003-04-2021
    313002-05-2021 606502-05-2021
    414030-06-2021 10012730-06-2021


    Third one is after package #3 arrives:
    DusanVH_1-1628022876309.png

     

    package_noclient_IDpackage_weightdue_date arr_daterunning_sum_whtProjectionProj_date
    01001-03-202101-03-20210  
    111030-03-202115-03-202110  
    212015-04-202103-04-202130  
    313002-05-202120-04-2021606020-04-2021
    414030-06-2021 10021430-06-2021



    To create the projection here I got the values by simply calculating the slope for the line between the last and penultimate arrival dates and multiplying that by the difference in days between the corresponding next date and the period start date. The dates for each value is the corresponding due_date.

    As you can see, the projection "advances" and gets updated each time a package arrives.

Thank you very much for your time, you're of great help.

Edit: added missing columns in the tables

Helpful resources

Announcements
PBI_User Group Leader_768x460.jpg

Manage your user group events

Check out the News & Announcements to learn more.

Power BI October Update 2021.jpg

Power BI Release

Click here to read more about the October 2021 Release!

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

Teds Dev Camp Oct. 2021 768x460.jpg

Power BI Dev Camp - October 28th, 2021

Mark your calendars and join us for our next Power BI Dev Camp!