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
Anonymous
Not applicable

Calculate sum of values between specific rows

I'm stuck trying to figure out how to solve this problem and I really hope you can help me.

 

I have two tables :

  • Code Fusion
SiteLineElement positionElementCampaignCode
GRA1ABP0251TUBE2GRA1ABP0251TUBE2
GRA1ABP0251TUBE6GRA1ABP0251TUBE6
GRA1ABP0253COUD3GRA1ABP0253COUD3
GRA1ABP0253COUD7GRA1ABP0253COUD7
GRA1ACO0033TUBE1GRA1ACO0033TUBE1
GRA1ACO0033TUBE5GRA1ACO0033TUBE5
GRA1ACO0035REUC4GRA1ACO0035REUC4
GRA1ACO0035REUC7GRA1ACO0035REUC7
  • Campaign duration
SiteLineElement positionElementCampaignCodeCampaign duration (h)
GRA1ABP0251TUBE1GRA1ABP0251TUBE16584
GRA1ABP0251TUBE2GRA1ABP0251TUBE26958
GRA1ABP0251TUBE3GRA1ABP0251TUBE36523
GRA1ABP0251TUBE4GRA1ABP0251TUBE46215
GRA1ABP0251TUBE5GRA1ABP0251TUBE57115
GRA1ABP0251TUBE6GRA1ABP0251TUBE64885
GRA1ABP0251TUBE7GRA1ABP0251TUBE76523
GRA1ABP0251TUBE8GRA1ABP0251TUBE86654
GRA1ABP0253COUD1GRA1ABP0253COUD16656
GRA1ABP0253COUD2GRA1ABP0253COUD27154
GRA1ABP0253COUD3GRA1ABP0253COUD32656
GRA1ABP0253COUD4GRA1ABP0253COUD49845
GRA1ABP0253COUD5GRA1ABP0253COUD56225
GRA1ABP0253COUD6GRA1ABP0253COUD66944
GRA1ABP0253COUD7GRA1ABP0253COUD72354
GRA1ACO0033TUBE1GRA1ACO0033TUBE16644
GRA1ACO0033TUBE2GRA1ACO0033TUBE26666
GRA1ACO0033TUBE3GRA1ACO0033TUBE36774
GRA1ACO0033TUBE4GRA1ACO0033TUBE46885
GRA1ACO0033TUBE5GRA1ACO0033TUBE56445
GRA2ACO0043TUBE6GRA2ACO0043TUBE68554
GRA1ACO0035REUC1GRA1ACO0035REUC12466
GRA1ACO0035REUC2GRA1ACO0035REUC25484
GRA1ACO0035REUC3GRA1ACO0035REUC33524
GRA1ACO0035REUC4GRA1ACO0035REUC44965
GRA1ACO0035REUC5GRA1ACO0035REUC54512
GRA1ACO0035REUC6GRA1ACO0035REUC66577
GRA1ACO0035REUC7GRA1ACO0035REUC78454

 

The current relationship between the two table are:

Code Fusion          Campaign duration

Code               1:*   Code

 

The column Code is a column I created in Power Query by simply combining Site & Line & Element position & Element & Campaign. I want to calculate a sum of the [Campaign duration] between the campaigns that are present in 'Code Fusion' as I filter the site, the line and the element with slicers.

 

For example, when I filter with the slicers 'Code Fusion' [Site] = GRA1, 'Code Fusion' [Line] = ABP025 and 'Code Fusion' [Element] = TUBE, I would want to have a measure that will calculate the sum of of the campaign duration from campaign 2 to 6 ( = 6958 + 6523 + 6215 + 7115 + 4885 = 31696 )

 

But it gets tricky because since I have a one-to-many relationship, when I filter with 'Code Fusion' it will only keep the lines of the campaign 2 and 6 of TUBE in position 1 of ABP025 for example. I tried to do a measure like this : CALCULATE(SUM('Campaign duration'[Campaign duration)]), FILTER(ALL('Campaign duration'), 'Campaign duration'[Campaign] >= MIN('Code Fusion'[Campaign]) && 'Campaign duration'[Campaign duration] <= MAX('Code Fusion'[Campaign])))

 

But it will sum up all the campaigns from 2 to 6 from every site and every line and every element.

 

I hope my explanation is clear enough and there is a way out of this problem. Thank you in advance for all your help !

1 ACCEPTED SOLUTION
jgeddes
Super User
Super User

If @tamerj1 s solution does not work you can try the measure as follows.

Duration =
var _site =
SELECTEDVALUE('Code Fusion'[Site])
var _line =
SELECTEDVALUE('Code Fusion'[Line])
var _element =
SELECTEDVALUE('Code Fusion'[Element])
var _codeTable =
ADDCOLUMNS('Code Fusion', "_code", RIGHT('Code Fusion'[Code],1))
var _lowerCampaign =
MINX(ADDCOLUMNS('Code Fusion', "_code", RIGHT('Code Fusion'[Code],1)),[_code])
var _upperCampaign =
MAXX(ADDCOLUMNS('Code Fusion', "_code", RIGHT('Code Fusion'[Code],1)),[_code])
return
CALCULATE(
    SUM('Campaign duration'[Campaign duration (h)]),
    FILTER(all('Campaign duration'), 'Campaign duration'[Site] = _site && 'Campaign duration'[Line] = _line && 'Campaign duration'[Element] = _element && 'Campaign duration'[Campaign] >= _lowerCampaign && 'Campaign duration'[Campaign] <= _upperCampaign)
)



Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





View solution in original post

3 REPLIES 3
jgeddes
Super User
Super User

If @tamerj1 s solution does not work you can try the measure as follows.

Duration =
var _site =
SELECTEDVALUE('Code Fusion'[Site])
var _line =
SELECTEDVALUE('Code Fusion'[Line])
var _element =
SELECTEDVALUE('Code Fusion'[Element])
var _codeTable =
ADDCOLUMNS('Code Fusion', "_code", RIGHT('Code Fusion'[Code],1))
var _lowerCampaign =
MINX(ADDCOLUMNS('Code Fusion', "_code", RIGHT('Code Fusion'[Code],1)),[_code])
var _upperCampaign =
MAXX(ADDCOLUMNS('Code Fusion', "_code", RIGHT('Code Fusion'[Code],1)),[_code])
return
CALCULATE(
    SUM('Campaign duration'[Campaign duration (h)]),
    FILTER(all('Campaign duration'), 'Campaign duration'[Site] = _site && 'Campaign duration'[Line] = _line && 'Campaign duration'[Element] = _element && 'Campaign duration'[Campaign] >= _lowerCampaign && 'Campaign duration'[Campaign] <= _upperCampaign)
)



Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





tamerj1
Super User
Super User

Hi @Anonymous 
Thank you for this clear explanation. 
This can be solved if we work it the other way around. Actually I need Campaign duration table to filter Code Fusion not the opposite. Sounds strange? Maybe but this will provide a smooth solution to your problem. Please let me know if there are no other restrictions against this to decide whether to proceed or think of other solutions.

Thank you

Anonymous
Not applicable

Hi, thanks for replying ! Unfortunately the table Code Fusion is the one that lists the campaigns that we have other interesting data on and that we actually study. So if we filter with Campaign duration, it would be like going over 1000 types of campaigns without knowing which one has interesting data. I hope it's clear for you 🙂

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.