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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Gazi_Sohan
Helper I
Helper I

Calculate with Filtering on AccountID (hardcoded) is working but not working on AccountID (dynamic)

Hi everyone. I have a created a DAX measures in which there is a filtering on Account ID column of COADetails table. 

Now the issue is that, in the measure, when I write the Account ID numbers as hardcoded, the measure works, but when I write those dynamically, it doesn't work. To avoid confusion, I have created the same DAX measure twice, one being hardcoded and the other being dynamic - 

Gazi_Sohan_0-1705087545712.png

I am providing the necessary files i.e. the datasource (excel file) and the Power BI report which you will get in the below link - 
https://drive.google.com/drive/folders/1xDiFgNOm_OpwcqfcgNhInF1homob_suP?usp=sharing


Can anybody please tell me what am I doing wrong ?

1 ACCEPTED SOLUTION

Hey @Anonymous 

I have tried my own way by using a pretty simple approach i.e. using selectedvalue function and it's working perfect in the measure and made it dynamic - 

Gazi_Sohan_0-1706294263976.png

I have applied the same concept in the sample POC file that I provided for you (attached) - 

Gazi_Sohan_1-1706294761609.png


and it's also working fine in the sample file - 

Gazi_Sohan_2-1706294887468.png

But anyway, thanks for your help. I really appreciate it. You are a very talneted guy I must say.
Hats off 💥


.pbix file with my dynamic measure - 
https://drive.google.com/file/d/1jRhL9q_ytVP4OxSyjntFliLqdm_IXynP/view?usp=sharing

View solution in original post

5 REPLIES 5
Anonymous
Not applicable

Hi, @Gazi_Sohan 

here is my solution:

The condition you set in the measure's calculate function is incorrect, we also need to restrict the Category. To solve this wrong, use the following DAX expression:

Dynamic Accounts = 
var selectedrecord = SELECTEDVALUE('COA Summary'[Account ID])
RETURN
SWITCH(
    selectedrecord,
    106,
    CALCULATE(
        SUM(Transactions[Amount]),
        ALL('COA Details'),
        'COA Details'[Category]="Revenues"&&
        'COA Details'[Account ID] >= 'COA Details'[Operator Between Start]
        && 'COA Details'[Account ID] <= 'COA Details'[Operator Between End]
    ),
    206,
    CALCULATE(
        SUM(Transactions[Amount]),
        ALL('COA Details'),
        'COA Details'[Category]="Expenses"&&
        'COA Details'[Account ID] >= 'COA Details'[Operator Between Start]
        && 'COA Details'[Account ID] <= 'COA Details'[Operator Between End]
    ),
    CALCULATE(
        SUM(Transactions[Amount]),
        'COA Details'[Account ID] >= 'COA Details'[Operator Between Start]
        && 'COA Details'[Account ID] <= 'COA Details'[Operator Between End]
    )
)

A screenshot of the formula is shown below:

2.png

The results are as follows:

3.png

This way we can get the correct Total Expenses and Total Revenues by dynamic measure.

 

 

 

How to Get Your Question Answered Quickly

If it does not help, please provide more details with your desired output and pbix file without privacy information (or some sample data) .

Best Regards

Jianpeng Li

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

Hey @Anonymous 

I have tried my own way by using a pretty simple approach i.e. using selectedvalue function and it's working perfect in the measure and made it dynamic - 

Gazi_Sohan_0-1706294263976.png

I have applied the same concept in the sample POC file that I provided for you (attached) - 

Gazi_Sohan_1-1706294761609.png


and it's also working fine in the sample file - 

Gazi_Sohan_2-1706294887468.png

But anyway, thanks for your help. I really appreciate it. You are a very talneted guy I must say.
Hats off 💥


.pbix file with my dynamic measure - 
https://drive.google.com/file/d/1jRhL9q_ytVP4OxSyjntFliLqdm_IXynP/view?usp=sharing

@Anonymous 
Thanks for your reply. 

In reality, we can't use the Category column because in the real report/file, we have to add Net Income which is not as easy as revenue/expense that you have shown in the filtering portion. Net Income is a combination of 7-8 categories including some blank categories in our main file.

We can't do this in our main file as because we only do have lots of Account Sub Categories when taken into consideration for calculating Net Income (Account Sub Category in the picture is basically the same as Category in the sample file that I provided) - 

Gazi_Sohan_0-1705730810333.png

Just like Revenue and Expenses (you included Category = "Revenue" and Category = "Expenses" within the calculate function), we can't do the same for Net Income (Category = "Net Income") as because it's not straightforward like Revenue and Expenses. Instead, Net Income is a combination of lots of categories as shown in the picture, it also includes blanks.

Also, if we use Category as a filter, then we actually don't need this part at all - 

'COA Details'[Account ID] >= 'COA Details'[Operator Between Start]
        && 'COA Details'[Account ID] <= 'COA Details'[Operator Between End]

because Category filter will automatically filter the accounts underneath.

Summary is - We can't use Category as a filter, instead we must have to anyhow use the AccountID, but it should not be hardcoded rather dynamic

lbendlin
Super User
Super User

What's the actual issue with the "Hardcoded Accounts"  measure?  Seems to be a reasonable approach.

Hey @lbendlin 
Sorry for the late reply to your question. Hardcoded means using raw numbers, for example, under the calculate function, we are using AccountID >= 400000 && AccountID <= 957090. Here 400000 and 957090 - these 2 are hardcoded numbers. 

Now, if you download the sample files, you will see that this numbers are present in the COA Details table under 2 columns - Operator Between Start and Operator Between End.

So, the summary is - we don't wan't to use AccountID >= 400000 && AccountID <= 957090 inside our measure, Instead we want to use AccountID >= Operator Between Start && AccountID <= Operator Between End.

Btw, we can't use the Category column and I explained the reason why (see my other replies). 

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

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

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

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