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

See when key Fabric features will launch and what’s already live, all in one place and always up to date. Explore the new Fabric roadmap

Reply
NWWill
Frequent Visitor

Reverse Running Total based On Another Column

I've got 4 columns:
date,
ContractCode,
SALES,
WIP

DateContract CodeSALESWIP
29/03/20246001,543,706.1610,209,064.12
05/04/20246001,309,658.440.00
12/04/20246001,628,801.660.00
19/04/20246001,553,039.340.00
26/04/20246001,551,622.008,174,078.70
03/05/20246001,534,546.60449,940.00
10/05/20246001,217,576.400.00
17/05/20246001,569,600.000.00
24/05/20246001,498,596.520.00
31/05/20246001,153,806.007,147,211.20
07/06/20246001,591,384.001,252,049.00
14/06/20246001,515,768.400.00
21/06/20246001,362,323.980.00
28/06/20246001,326,576.008,540,147.66
05/07/20246001,468,997.020.00
12/07/20246001,486,834.540.00
19/07/20246001,463,386.620.00
26/07/20246001,411,629.880.00
02/08/20246001,384,225.9410,612,779.26
09/08/20246001,428,952.88159,268.00
16/08/20246001,402,028.180.00
23/08/20246001,477,901.140.00
30/08/20246001,129,457.165,413,048.61
06/09/20246001,294,089.053,313.19
13/09/20246001,271,300.550.00
20/09/20246001,191,459.420.00

 

I'm currently struggling to calculate FirstWeekToMeetTarget.

Here’s how the FirstWeekToMeetTarget is calculated step by step:

  1. Identify the Target (WIP):

    • For each row, check the value of WIP.
    • If the value is non-zero or non-blank, that row becomes the target for the calculation.
  2. Reverse Cumulative Sales Calculation:

    • Starting from the current week (row with the target WIP), sum up the SALES values backward in time (week by week).
    • Continue summing until the cumulative sales meet or exceed the target value (WIP).
  3. Determine the First Week:

    • The FirstWeekToMeetTarget is the earliest week (while summing backwards) where the cumulative sales first meet or exceed the target value.
    • If no week meets the target, the result will remain blank.

Here is the expected output (The dataset is a sample of one ContractCode, there are many other ContractCode):

DateContract CodeSALESWIPFirstWeekToMeetTarget
29/03/20246001,543,706.1610,209,064.12 
05/04/20246001,309,658.440.00 
12/04/20246001,628,801.660.00 
19/04/20246001,553,039.340.00 
26/04/20246001,551,622.008,174,078.70 
03/05/20246001,534,546.60449,940.0003/05/2024
10/05/20246001,217,576.400.00 
17/05/20246001,569,600.000.00 
24/05/20246001,498,596.520.00 
31/05/20246001,153,806.007,147,211.2026/04/2024
07/06/20246001,591,384.001,252,049.0007/06/2024
14/06/20246001,515,768.400.00 
21/06/20246001,362,323.980.00 
28/06/20246001,326,576.008,540,147.6617/05/2024
05/07/20246001,468,997.020.00 
12/07/20246001,486,834.540.00 
19/07/20246001,463,386.620.00 
26/07/20246001,411,629.880.00 
02/08/20246001,384,225.9410,612,779.2614/06/2024
09/08/20246001,428,952.88159,268.0009/08/2024
16/08/20246001,402,028.180.00 
23/08/20246001,477,901.140.00 
30/08/20246001,129,457.165,413,048.6109/08/2024
06/09/20246001,294,089.053,313.1906/09/2024
13/09/20246001,271,300.550.00 
20/09/20246001,191,459.420.00

 



FYI - The reason why there's no date for these two rows is becuase stakeholder has given me a sample dataset, the data for this particular ContractCode only goes back as far as 29/03/204 but once I have the full dataset, I expect the calulcation to populate these rows

DateContract CodeSALESWIPFirstWeekToMeetTarget
29/03/20246001,543,706.1610,209,064.12 
26/04/20246001,551,622.008,174,078.70 

 

Once I've got this calculated column correct I would like another calulcated column that counts the number of weeks it took SALES to >= Wip
e.g. 

DateContractCodeSALESWIPFirstWeekToMeetTarget
02/08/20246001,384,225.9410,612,779.2614/06/2024

This took 8 weeks of SALES to >= Wip

 

DateContractCodeSALESWIPFirstWeekToMeetTarget
06/09/20246001,294,089.053,313.1906/09/2024

This took 1 week of @SALES to >= @Wip

 

This is my current calulated table formula:

 

 

 

 

Table = 
VAR TempTable1 =
    ADDCOLUMNS(
        SUMMARIZECOLUMNS(
            'WIP & Debt Data'[Date],
            'WIP & Debt Data'[Contract Code],
            "@Sales", SUM('WIP & Debt Data'[Sales]),
            "@WIP", SUM('WIP & Debt Data'[Closing WIP & Debt])
        ),
        "CumulativeSales",
        VAR CurrentDate = [Date]
        VAR CurrentContract = [Contract Code]
        RETURN
            CALCULATE(
                SUM('WIP & Debt Data'[Sales]),
                FILTER(
                    ALL('WIP & Debt Data'),
                    'WIP & Debt Data'[Date] <= CurrentDate &&
                    'WIP & Debt Data'[Contract Code] = CurrentContract
                )
            )
    )

VAR ResultTable =
    ADDCOLUMNS(
        TempTable1,
        "FirstWeekToMeetTarget", 
VAR TargetWIPDebt = [@WIP]
VAR CurrentDate = [Date]
VAR CurrentContract = [Contract Code]
RETURN
    IF (
        TargetWIPDebt > 0,
        CALCULATE(
            MINX(
                FILTER(
                    ALLSELECTED('WIP & Debt Data'),
                    'WIP & Debt Data'[Contract Code] = CurrentContract
                    && 'WIP & Debt Data'[Date] <= CurrentDate
                ),
                'WIP & Debt Data'[Date]
            ),
            FILTER (
                ALLSELECTED ( 'WIP & Debt Data' ),
                'WIP & Debt Data'[Contract Code] = CurrentContract
                && CALCULATE (
                    SUM ( 'WIP & Debt Data'[Sales] ),
                    FILTER (
                        ALLSELECTED ( 'WIP & Debt Data' ),
                        'WIP & Debt Data'[Date] <= EARLIER ( 'WIP & Debt Data'[Date] )
                    )
                ) >= TargetWIPDebt
            )
        )
    )
    )
RETURN
    FILTER(
        ResultTable,
        NOT ISBLANK([@WIP&Debt]) // Filter out rows with blank WIP & Debt
    )

 

 

 

 

Thank you

2 ACCEPTED SOLUTIONS

lbendlin_1-1733536622846.png

 

 

 

 

FirstWeekToMeetTarget = 
var w = max('Table'[WIP])
return if(ISBLANK(w),BLANK(),
var wk = max('Table'[Date])
var a = CALCULATETABLE('Table',REMOVEFILTERS('Table'[SALES],'Table'[WIP]),'Table'[Date]<=wk)
var b = ADDCOLUMNS(a,"cm",var wkd = [Date] return sumx(filter(a,[Date]>=wkd),[SALES]))
var c = topn(1,filter(b,[cm]>=w),[Date],DESC)
return CONCATENATEX(c,[Date])
)

 

View solution in original post

You should be able to add that yourself. filter all week values to include the interval from the FirstWeekToMeetTarget to the current week, and return the count of rows.  Minus one if you don't want to count the current week.

View solution in original post

7 REPLIES 7
Anonymous
Not applicable

Hi @NWWill ,

Have you solved your problem? If so, can you share your solution here and mark the correct answer as a standard answer to help other members find it faster?

 

 

Best Regards

Yilong Zhou

NWWill
Frequent Visitor

I've got 4 columns:
date,
ContractCode,
SALES,
WIP

DateContract CodeSALESWIP
29/03/20246001,543,706.1610,209,064.12
05/04/20246001,309,658.440.00
12/04/20246001,628,801.660.00
19/04/20246001,553,039.340.00
26/04/20246001,551,622.008,174,078.70
03/05/20246001,534,546.60449,940.00
10/05/20246001,217,576.400.00
17/05/20246001,569,600.000.00
24/05/20246001,498,596.520.00
31/05/20246001,153,806.007,147,211.20
07/06/20246001,591,384.001,252,049.00
14/06/20246001,515,768.400.00
21/06/20246001,362,323.980.00
28/06/20246001,326,576.008,540,147.66
05/07/20246001,468,997.020.00
12/07/20246001,486,834.540.00
19/07/20246001,463,386.620.00
26/07/20246001,411,629.880.00
02/08/20246001,384,225.9410,612,779.26
09/08/20246001,428,952.88159,268.00
16/08/20246001,402,028.180.00
23/08/20246001,477,901.140.00
30/08/20246001,129,457.165,413,048.61
06/09/20246001,294,089.053,313.19
13/09/20246001,271,300.550.00
20/09/20246001,191,459.420.00

 

I'm currently struggling to calculate FirstWeekToMeetTarget.

Here’s how the FirstWeekToMeetTarget is calculated step by step:

  1. Identify the Target (WIP):

    • For each row, check the value of WIP.
    • If the value is non-zero or non-blank, that row becomes the target for the calculation.
  2. Reverse Cumulative Sales Calculation:

    • Starting from the current week (row with the target WIP), sum up the SALES values backward in time (week by week).
    • Continue summing until the cumulative sales meet or exceed the target value (WIP).
  3. Determine the First Week:

    • The FirstWeekToMeetTarget is the earliest week (while summing backwards) where the cumulative sales first meet or exceed the target value.
    • If no week meets the target, the result will remain blank.

Here is the expected output (The dataset is a sample of one ContractCode, there are many other ContractCode):

DateContract CodeSALESWIPFirstWeekToMeetTarget
29/03/20246001,543,706.1610,209,064.12 
05/04/20246001,309,658.440.00 
12/04/20246001,628,801.660.00 
19/04/20246001,553,039.340.00 
26/04/20246001,551,622.008,174,078.70 
03/05/20246001,534,546.60449,940.0003/05/2024
10/05/20246001,217,576.400.00 
17/05/20246001,569,600.000.00 
24/05/20246001,498,596.520.00 
31/05/20246001,153,806.007,147,211.2026/04/2024
07/06/20246001,591,384.001,252,049.0007/06/2024
14/06/20246001,515,768.400.00 
21/06/20246001,362,323.980.00 
28/06/20246001,326,576.008,540,147.6617/05/2024
05/07/20246001,468,997.020.00 
12/07/20246001,486,834.540.00 
19/07/20246001,463,386.620.00 
26/07/20246001,411,629.880.00 
02/08/20246001,384,225.9410,612,779.2614/06/2024
09/08/20246001,428,952.88159,268.0009/08/2024
16/08/20246001,402,028.180.00 
23/08/20246001,477,901.140.00 
30/08/20246001,129,457.165,413,048.6109/08/2024
06/09/20246001,294,089.053,313.1906/09/2024
13/09/20246001,271,300.550.00 
20/09/20246001,191,459.420.00

 



FYI - The reason why there's no date for these two rows is becuase stakeholder has given me a sample dataset, the data for this particular ContractCode only goes back as far as 29/03/204 but once I have the full dataset, I expect the calulcation to populate these rows

DateContract CodeSALESWIPFirstWeekToMeetTarget
29/03/20246001,543,706.1610,209,064.12 
26/04/20246001,551,622.008,174,078.70 

 

Once I've got this calculated column correct I would like another calulcated column that counts the number of weeks it took SALES to >= Wip
e.g. 

DateContractCodeSALESWIPFirstWeekToMeetTarget
02/08/20246001,384,225.9410,612,779.2614/06/2024

This took 8 weeks of SALES to >= Wip

 

DateContractCodeSALESWIPFirstWeekToMeetTarget
06/09/20246001,294,089.053,313.1906/09/2024

This took 1 week of @SALES to >= @Wip

 

This is my current calulated table formula:

 

 

Table = 
VAR TempTable1 =
    ADDCOLUMNS(
        SUMMARIZECOLUMNS(
            'WIP & Debt Data'[Date],
            'WIP & Debt Data'[Contract Code],
            "@Sales", SUM('WIP & Debt Data'[Sales]),
            "@WIP", SUM('WIP & Debt Data'[Closing WIP & Debt])
        ),
        "CumulativeSales",
        VAR CurrentDate = [Date]
        VAR CurrentContract = [Contract Code]
        RETURN
            CALCULATE(
                SUM('WIP & Debt Data'[Sales]),
                FILTER(
                    ALL('WIP & Debt Data'),
                    'WIP & Debt Data'[Date] <= CurrentDate &&
                    'WIP & Debt Data'[Contract Code] = CurrentContract
                )
            )
    )

VAR ResultTable =
    ADDCOLUMNS(
        TempTable1,
        "FirstWeekToMeetTarget", 
VAR TargetWIPDebt = [@WIP]
VAR CurrentDate = [Date]
VAR CurrentContract = [Contract Code]
RETURN
    IF (
        TargetWIPDebt > 0,
        CALCULATE(
            MINX(
                FILTER(
                    ALLSELECTED('WIP & Debt Data'),
                    'WIP & Debt Data'[Contract Code] = CurrentContract
                    && 'WIP & Debt Data'[Date] <= CurrentDate
                ),
                'WIP & Debt Data'[Date]
            ),
            FILTER (
                ALLSELECTED ( 'WIP & Debt Data' ),
                'WIP & Debt Data'[Contract Code] = CurrentContract
                && CALCULATE (
                    SUM ( 'WIP & Debt Data'[Sales] ),
                    FILTER (
                        ALLSELECTED ( 'WIP & Debt Data' ),
                        'WIP & Debt Data'[Date] <= EARLIER ( 'WIP & Debt Data'[Date] )
                    )
                ) >= TargetWIPDebt
            )
        )
    )
    )
RETURN
    FILTER(
        ResultTable,
        NOT ISBLANK([@WIP&Debt]) // Filter out rows with blank WIP & Debt
    )

 

 

Thank you

Sales never catch up.

 

lbendlin_0-1733259467440.png

 

Hi Ibendlin,

Thank you for you comment.

 

I think you may have missunderstood what I'm trying to achieve. To summarise and simplify what I'm trying to achieve is,


I have a table with 4 columns:

  • Date (each row represents one week)
  • Contract (let ignore this column for now)
  • Sales
  • WIP

The table is sorted by Date Asc

 

whenever the WIP value is greater than 0 , starting from the same row, I want to sum the Sales column going back in time till the Sales value is greater or equal to the WIP in question. 

This needs to occur everytime WIP value is greater than 0.

 

I want the output to display how many weeks Sales took to equal or be greater than WIP 

 

when WIP = 7,147,211.20, it took Sales (Summing backwards in time)  6 weeks to >= to WIP

when WIP = 449,940, it took Sales (summing backwards in time) 1 week to >= to WIP

and so on... 


Screenshot 2024-12-05 093003.png

lbendlin_1-1733536622846.png

 

 

 

 

FirstWeekToMeetTarget = 
var w = max('Table'[WIP])
return if(ISBLANK(w),BLANK(),
var wk = max('Table'[Date])
var a = CALCULATETABLE('Table',REMOVEFILTERS('Table'[SALES],'Table'[WIP]),'Table'[Date]<=wk)
var b = ADDCOLUMNS(a,"cm",var wkd = [Date] return sumx(filter(a,[Date]>=wkd),[SALES]))
var c = topn(1,filter(b,[cm]>=w),[Date],DESC)
return CONCATENATEX(c,[Date])
)

 

Thanks this worked to get the date of the FirstWeekToMeetTarget. However, instead of showing the date, I want to display how many weeks in INT it took to reach FirstWeekToMeetTarget 

eg.

1. when WIP = 7,147,211.20, it took Sales (Summing backwards in time)  6 weeks to >= to WIP  Output = 6

 

2. when WIP = 449,940, it took Sales (summing backwards in time) 1 week to >= to WIP
Output = 1

Thanks for the guidance so far!

You should be able to add that yourself. filter all week values to include the interval from the FirstWeekToMeetTarget to the current week, and return the count of rows.  Minus one if you don't want to count the current week.

Helpful resources

Announcements
May PBI 25 Carousel

Power BI Monthly Update - May 2025

Check out the May 2025 Power BI update to learn about new features.

May 2025 Monthly Update

Fabric Community Update - May 2025

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