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
gandharv
Frequent Visitor

Calculated column using DAX

Hi Guys, 

I need urgent help with the DAX function to calculate column H of the below dummy data table in Power BI. 

Important to consider :

The Formula should consider Column A, B, C, and D as unique for each row, since as you can see that column H is calculated based on value of a column G in the current row + value of column H in the earlier row (within same model_run, category, date, but earlier time stamp). 

 

gandharv_1-1703159901497.png

 

Appreciate your help!

 

Best Regards,

 

 

1 ACCEPTED SOLUTION

Hi, @gandharv 

in above you mention price-cost  and you tack cost-price(f2-e2) 

so i take as a cost-price(f2-e2) so look at these

try below code with some changes

column= 
var a = tablename[model_run]
var b = tablename[category]
var c = tablename[time]
var e = tablename[date]
return
sumx(
  filter(
  tablename,
  tablename[model_run]=a && tablename[category]=b && tablename[time]<= c && tablename[time]=e
  ),
   tablename[cost]-tablename[price]

)

 

 

View solution in original post

11 REPLIES 11
Dangar332
Super User
Super User

Hi, @gandharv 

try below

column =
var a = tablename[model_run]
var b = tablename[category]
var c = tablename[time]
var d = tablename[cost]-tablename[price]
return
sumx(
  filter(
  tablename,
  tablename[model_run]=a && tablename[category]=b && tablename[time]<= c
  ),
  d 
)

Hi Dangar, Thanks for the reply. but your query doesn't seem to consider the Date Column. 

HI, @gandharv 

as per your requirnment it not necessary to use date column

try above code check you get your expected output or not?

Sorry mate, if I misrepresented the Date column by keeping it the same date for all rows. But in reality, I have dates from 1/1/23 to 31/12/26, for each Category with Timestamp of 24 hour for each date. 

Hi, @gandharv 

 

column =
var a = tablename[model_run]
var b = tablename[category]
var c = tablename[time]
var d = tablename[cost]-tablename[price]
var e = tablename[date]
return
sumx(
  filter(
  tablename,
  tablename[model_run]=a && tablename[category]=b && tablename[time]<= c && tablename[time]=e
  ),
  d 
)

Hi, thanks again, I applied it but the result is wrong, In below screenshot, the Carry forward column has the correct result (manually done in excel) and the calculated column is using ur DAX. 

gandharv_0-1703168996585.png

 

Hi, @gandharv 

in above you mention price-cost  and you tack cost-price(f2-e2) 

so i take as a cost-price(f2-e2) so look at these

try below code with some changes

column= 
var a = tablename[model_run]
var b = tablename[category]
var c = tablename[time]
var e = tablename[date]
return
sumx(
  filter(
  tablename,
  tablename[model_run]=a && tablename[category]=b && tablename[time]<= c && tablename[time]=e
  ),
   tablename[cost]-tablename[price]

)

 

 

Hi Dangar, 

Thank you for your help earlier. I have realised some changes in my original dataset, herewith am attaching a screenshot of dummy data which is the actual representation of my real data. Can you pls help me modify the DAX function accordingly? I have manually calculated in Excel for your clear understanding and mentioned the condition as well. 

 

gandharv_0-1704281295586.png

 

hi, @gandharv 

try below

but cross check it before move  ahead

 

Column = 
var a =
    FILTER('Table (2)',
           'Table (2)'[Model_Run]=EARLIER('Table (2)'[Model_Run]) &&
           'Table (2)'[Area] = EARLIER('Table (2)'[Area]) &&
           'Table (2)'[Date] = EARLIER('Table (2)'[Date]) &&
           'Table (2)'[Time]<= EARLIER('Table (2)'[Time])
    )
var b = MINX('Table (2)','Table (2)'[Time])
var d = MINX(FILTER('Table (2)','Table (2)'[Time]=b),'Table (2)'[Price - Cost (Value)])
var c = SUMX(a,'Table (2)'[Price - Cost (Value)])-d
return
IF(c<0,0,c)

 

 

Dangar332_0-1704299505626.png

If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.

Hi, @gandharv 

please paste data here or provide data file

 

Model_RunAreaDateTimePriceCostPrice - Cost (Value)Carry forward (Value)Carry forward (Formula) 
1A1/1/202300:00100150-500=IF(G2 < 0, 0, G2)Condition for the column: Carry Forward shouldn't be less than 0
1A1/1/202300:152001505050=IF(G3+H2 < 0, 0, G3+H2) 
1A1/1/202300:30250150100150=IF(G4+H3 < 0, 0, G4+H3) 
1A1/1/202300:45100150-50100=IF(G5+H4 < 0, 0, G5+H4) 
1A1/1/202301:0050150-1000=IF(G6+H5 < 0, 0, G6+H5) 
1A1/1/202301:1530150-1200=IF(G7+H6 < 0, 0, G7+H6) 
1A2/2/202300:00100150-500=IF(G8 < 0, 0, G8)Condition: Running total should restart as the Date changes
1A2/2/202300:152001505050=IF(G9+H8 < 0, 0, G9+H8) 
1A2/2/202300:30100150-500=IF(G10+H9 < 0, 0, G10+H9) 
1B1/1/202300:00100150-500=IF(G11 < 0, 0, G11)Condition: Running total should restart as the Area and Date changes
1B1/1/202300:152001505050=IF(G12+H11 < 0, 0, G12+H11) 
1B1/1/202300:30250150100150=IF(G13+H12 < 0, 0, G13+H12) 
2A1/1/202300:00100150-500=IF(G14 < 0, 0, G14)Condition: Running total should restart as the Model_Run, Area and Date changes
2A1/1/202300:152001505050=IF(G15+H14 < 0, 0, G15+H14) 
2A1/1/202300:30250150100150=IF(G16+H15 < 0, 0, G16+H15) 
2A1/1/202300:45100150-50100=IF(G17+H16 < 0, 0, G17+H16) 
2A1/1/202301:0050150-1000=IF(G18+H17 < 0, 0, G18+H17) 
2A1/1/202301:1530150-1200=IF(G19+H18 < 0, 0, G19+H18) 
2A2/2/202300:00100150-500=IF(G20 < 0, 0, G20)Condition: Running total should restart as the Date changes
2A2/2/202300:152001505050=IF(G21+H20 < 0, 0, G21+H20) 
2A2/2/202300:30100150-500=IF(G22+H21 < 0, 0, G22+H21) 
2B1/1/202300:00100150-500=IF(G23 < 0, 0, G23)Condition: Running total should restart as the Area and Date changes
2B1/1/202300:152001505050=IF(G24+H23 < 0, 0, G24+H23) 
2B1/1/202300:30250150100150=IF(G25+H24 < 0, 0, G25+H24) 
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          

 

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.