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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
muhammadbinzia
Frequent Visitor

How to calculate SUMIFs in a measure for other measure values using earlier

So i have a table in Power BI That is composed of Measures by Credit Note ID, Description, Customer No. , Document Number, Bal and Total. 

Now i need to calculate sumif for these values such as 

 

Balance = ROUND(SWITCH(TRUE,[Credit Note ID]="NA"&&[Description]<>"Receipt",CALCULATE(SUM([Total]),FILTER(LPS,([Customer No.]=EARLIER([Customer No.]))&&([Document Number]=EARLIER([Document Number]))&&([Bal]=EARLIER([Bal]))))),0)
 
The problem is that these formulae (SUM and Earlier) only work on columns with row context but not on measures  .....    
 
If someone can help me with getting the formula that works on measures in the same way for SUMIF...for measures ...an example table is below.
 
Customer No.Document NumberDescriptionCredit Note IDBalTotal
1000008000001Bill NA1180,000
1000008000001ReceiptNA1-180,000
1000008000002BillNA1190,000
1000008000002ReceiptNA1-190,000
1000008000003Bill NA1110,000
1000008000003ReceiptNA2-50,000
1000018000001Bill NA1280,000
1000018000001ReceiptNA1-280,000
1000018000003BillNA1290,000
1000018000003ReceiptNA1-290,000
1000018000004Bill NA1310,000
1000018000004ReceiptNA2-150,000
1000028000004Bill NA1380,000
1000028000004ReceiptNA1-380,000
1000028000005BillNA1490,000
1000028000005ReceiptNA1-490,000
1000028000006Bill NA1410,000
1000028000006ReceiptNA2-250,000
      
      

 

12 REPLIES 12
v-jtian-msft
Community Support
Community Support

Hi, Ashish_Mathur ,Bibiano_Geraldo ,MFelix and DataNinja777 ,thanks for your concern about this issue.
And I would like to share some additional solutions below.
Hello,@muhammadbinzia .I am glad to help you.
Like this?

vjtianmsft_0-1732781104331.png

It should be noted that since the calculation logic of power BI dax is based on the whole row from top to bottom, and the order is not reversible, it is not possible to realize that after summing the data in the first row and the data in the second row, the final result is displayed in the first row. (dax requires that the computational logic be executed on each row, from top to bottom.)
Therefore, dax requires that the logic be executed on each row itself.
Like this.

vjtianmsft_1-1732781142074.png

So you need to use an IF judgment to mask the extra calculations
Here's my measure.

vjtianmsft_0-1732781268992.png

If you want to calculate the balance by creating a measure, the result will only be displayed in the row [Description] = “Receipt”, not in the row [Description] = “Bill”.
This is an effect of DAX's calculation logic (from top to bottom, the whole row is calculated, so you can not use DAX to achieve a true iterative cycle of settlement)

You can achieve a similar effect by creating indexes for labeling!
If you find my suggestions helpful, you can check out my tests below
1 Grouping data for labeling by Customer No. (sorting within groups)

vjtianmsft_1-1732781324314.png
Below is my M code:

let
    Source = Excel.Workbook(File.Contents("C:\Users\username\Desktop\test_11_27.xlsx"), null, true),
    LPS_Sheet = Source{[Item="LPS",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(LPS_Sheet, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Customer No.", Int64.Type}, {"Document Number", Int64.Type}, {"Description", type text}, {"Credit Note ID", type text}, {"Bal", Int64.Type}, {"Total", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Customer No."}, {{"GroupedData", each Table.AddIndexColumn(_, "Index", 0, 1, Int64.Type)}}),
    #"Expanded GroupedData" = Table.ExpandTableColumn(#"Grouped Rows", "GroupedData", {"Document Number", "Description", "Credit Note ID", "Bal", "Total", "Index"})
in
    #"Expanded GroupedData"

Depending on the size of the Index column to work with the SUM function to achieve an effect similar to iteration (from top to bottom. Compare the index of the current row with the index of the previous row, and add up the Total values of all the indexes smaller than the current row).
At the same time need to ensure that the following filter:

&&'LPS'[Customer No.] = _customerNO &&'LPS'[Document Number] =_doucmentNumber && _creditNoteID = "NA" 

Aggregate functions are commonly used in measure to save the current row of results.
Like this:

Var _index =MAX([index])

vjtianmsft_2-1732781535766.png


I hope my suggestions give you good ideas, if you have any more questions, please clarify in a follow-up reply.
Best Regards,
Carson Jian,
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Thank you for your help Sir, The Result that is required is like that below:

In this, it would be like the following table [Balance] measure ... Remember all the columns in this table are actually measures so need SUMIFs formula that is based on measures [Customer No.] && [Document Number] && [Bal] only for measures where measure [Credit Note ID] ="NA" && measure [Description] <> "Receipt". So the measure would have result like [Balance] (last column in below table)..The functions SUM(), MAX, SELECTEDVALUE(), EARLIER Only take column as arguments so not working for measures ...Thanks in advance in anticipation of a solution

 

Customer No.Document NumberDescriptionCredit Note IDBalTotalBalance
1000008000001Bill NA1180,0000
1000008000001ReceiptNA1-180,000 
1000008000002BillNA1190,0000
1000008000002ReceiptNA1-190,000 
1000008000003Bill NA1110,00060,000
1000008000003ReceiptNA2-50,000 
1000018000001Bill NA1280,0000
1000018000001ReceiptNA1-280,000 
1000018000003BillNA1290,0000
1000018000003ReceiptNA1-290,000 
1000018000004Bill NA1310,000160,000
1000018000004ReceiptNA2-150,000 
1000028000004Bill NA1380,0000
1000028000004ReceiptNA1-380,000 
1000028000005BillNA1490,0000
1000028000005ReceiptNA1-490,000 
1000028000006Bill NA1410,000160,000
1000028000006ReceiptNA2-250,000 
       

 

Ashish_Mathur
Super User
Super User

Hi,

Based on the table that you have shared, show the expected result.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

In this, it would be like the following table [Balance] measure ...Remember all the columns in this table are actually measures so need SUMIFs formula that is based on measures [Customer No.] && [Document Number] && [Bal] only for measures where measure [Credit Note ID] ="NA" && measure [Description] <> "Receipt". So the measure would have result like [Balance] (last column in below table)..The functions SUM(), MAX, SELECTEDVALUE(), EARLIER Only take column as arguments so not working for measures ...Thanks in advance in anticipation of a solution

 

Customer No.Document NumberDescriptionCredit Note IDBalTotalBalance
1000008000001Bill NA1180,0000
1000008000001ReceiptNA1-180,000 
1000008000002BillNA1190,0000
1000008000002ReceiptNA1-190,000 
1000008000003Bill NA1110,00060,000
1000008000003ReceiptNA2-50,000 
1000018000001Bill NA1280,0000
1000018000001ReceiptNA1-280,000 
1000018000003BillNA1290,0000
1000018000003ReceiptNA1-290,000 
1000018000004Bill NA1310,000160,000
1000018000004ReceiptNA2-150,000 
1000028000004Bill NA1380,0000
1000028000004ReceiptNA1-380,000 
1000028000005BillNA1490,0000
1000028000005ReceiptNA1-490,000 
1000028000006Bill NA1410,000160,000
1000028000006ReceiptNA2-250,000 
       

This measure pattern should work

Measure = calculate([Total],removefilters(Data,Data[Description],Data[Cedit Note ID],Data[Bal]))

Hope this helps.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
muhammadbinzia
Frequent Visitor

Customer No.Document NumberDescriptionCredit Note IDBalTotalBalance
1000008000001Bill NA1180,0000
1000008000001ReceiptNA1-180,000 
1000008000002BillNA1190,0000
1000008000002ReceiptNA1-190,000 
1000008000003Bill NA1110,00060,000
1000008000003ReceiptNA2-50,000 
1000018000001Bill NA1280,0000
1000018000001ReceiptNA1-280,000 
1000018000003BillNA1290,0000
1000018000003ReceiptNA1-290,000 
1000018000004Bill NA1310,000160,000
1000018000004ReceiptNA2-150,000 
1000028000004Bill NA1380,0000
1000028000004ReceiptNA1-380,000 
1000028000005BillNA1490,0000
1000028000005ReceiptNA1-490,000 
1000028000006Bill NA1410,000160,000
1000028000006ReceiptNA2-250,000 
       
       
Bibiano_Geraldo
Resident Rockstar
Resident Rockstar

Hi @muhammadbinzia ,

Please try the bellow DAX:

Balance Measure = 
VAR CurrentCustomer = SELECTEDVALUE(LPS[Customer No.])
VAR CurrentDocument = SELECTEDVALUE(LPS[Document Number])
VAR CurrentBal = SELECTEDVALUE(LPS[Bal])

RETURN
ROUND(
    SWITCH(
        TRUE(),
        SELECTEDVALUE(LPS[Credit Note ID]) = "NA" &&
        SELECTEDVALUE(LPS[Description]) <> "Receipt",
        SUMX(
            FILTER(
                LPS,
                LPS[Customer No.] = CurrentCustomer &&
                LPS[Document Number] = CurrentDocument &&
                LPS[Bal] = CurrentBal
            ),
            LPS[Total]
        ),
        0
    ),
0)

 

 

I hope this helps! 
If you found this answer helpful:
✔️ Mark it as the solution to help others find it faster.
 Give it a like to show your appreciation!

Thank you for contributing to our amazing Power BI community! 

Thanks for the response. However, the problem persists as SelectedValue Function it says doesnt take measures as argument but only columns...while all the upper given are measures and especially measure [Bal] which can only be a measure and not a column...So i dont understand which functions directly work with Measures , Especially in Function Earlier() Context

MFelix
Super User
Super User

Hi @muhammadbinzia ,

 

Try the following code:

LPS Balance value = 
VAR _tempTable = CALCULATETABLE(
			ALLSELECTED(LPS),
			LPS[Customer No.] = SELECTEDVALUE(LPS[Customer No.]) && LPS[Document Number] = SELECTEDVALUE(LPS[Document Number]) && LPS[Balance] = SELECTEDVALUE(LPS[Balance])
		)
		VAR _CreditNote = CALCULATETABLE(
			_tempTable,
			LPS[Credit Note ID] = "NA" && LPS[Description] <> "Receipt"
		)
		RETURN
			SUMX(
				_CreditNote,
				LPS[Balance]
			)

Regards

Miguel Félix


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

Proud to be a Super User!

Check out my blog: Power BI em Português



Thank you for you reply.  However, It is saying (A function 'PLACEHOLDER' has been used in a True/False expression that is used as a table filter expression. This is not allowed)...Also probably The Selected Value Function needs a column instead of measure so it is showing underlined red.  if any other solution can fixt it.

DataNinja777
Super User
Super User

@muhammadbinzia ,

 

You can replicate the functionality of SUMIF for measures using CALCULATE and FILTER. Here’s an alternative:

 

Balance Measure = 
ROUND(
    SUMX(
        FILTER(
            LPS, 
            [Credit Note ID] = "NA" &&
            [Description] <> "Receipt" &&
            [Customer No.] = MAX(LPS[Customer No.]) &&
            [Document Number] = MAX(LPS[Document Number]) &&
            [Bal] = MAX(LPS[Bal])
        ),
        LPS[Total]
    ),
    0
)

 

 

Best regards,

Thanks for the help, however, the MAX function used in it requires a column and doesnt accept measure

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.