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

Next up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now

Reply
Anonymous
Not applicable

Measure to count transactions for all hierarchy levels

Capture.JPG

Please help me create a measure that would count how many transactions there are for a given period for each hierarchical level of accounts. I am really stuck right now.

There are two tables - transactions and accounts. Each acount has a parent account and a calculated path(account table[account name]) column.

I was trying something like this, but this only show if the account itself has made a transaction and not if its child accounts have made transactions. This measure will be in a matrix, where I can see how many transactions does each level have.

 

Count of Transactions =
VAR selPeriod = SELECTEDVALUE(period table[period])
VAR selAccount = SELECTEDVALUE(account table[account name])
VAR sumforAccount = SUMX(transactions table, IF(transactions table[period]=selPeriod && transactions table[account name]=selAccount,1,0))

RETURN
IF(sumforAccount>0,sumforAccount,0)

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @Anonymous ,

 

I think Path column should be an Enter Data. Path function is not suitable in your data model.

It is better for you to disconnect Account table and Transaction table or create an unrelated DimAccount table, relationship will impact measure.

Try this code.

Count =
VAR _SELECTACCOUNT =
    SELECTEDVALUE ( Account[Account Name] )
VAR _PATH =
    SUMMARIZE ( ALLSELECTED ( Account ), Account[Account Name], Account[Path] )
VAR _Search =
    FILTER (
        ADDCOLUMNS ( _PATH, "Search", SEARCH ( _SELECTACCOUNT, [Path],, 0 ) ),
        [Search] <> 0
    )
VAR _ACCOUNTLIST =
    FILTER (
        SUMMARIZE ( _Search, [Account Name] ),
        [Account Name] IN VALUES ( 'Transaction'[Account Name] )
    )
VAR _COUNT =
    COUNTAX ( _ACCOUNTLIST, [Account Name] )
RETURN
    _COUNT

Result is as below.

1.png

Best Regards,
Rico Zhou

 

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

 

View solution in original post

4 REPLIES 4
Anonymous
Not applicable

Hi @Anonymous ,

 

I think Path column should be an Enter Data. Path function is not suitable in your data model.

It is better for you to disconnect Account table and Transaction table or create an unrelated DimAccount table, relationship will impact measure.

Try this code.

Count =
VAR _SELECTACCOUNT =
    SELECTEDVALUE ( Account[Account Name] )
VAR _PATH =
    SUMMARIZE ( ALLSELECTED ( Account ), Account[Account Name], Account[Path] )
VAR _Search =
    FILTER (
        ADDCOLUMNS ( _PATH, "Search", SEARCH ( _SELECTACCOUNT, [Path],, 0 ) ),
        [Search] <> 0
    )
VAR _ACCOUNTLIST =
    FILTER (
        SUMMARIZE ( _Search, [Account Name] ),
        [Account Name] IN VALUES ( 'Transaction'[Account Name] )
    )
VAR _COUNT =
    COUNTAX ( _ACCOUNTLIST, [Account Name] )
RETURN
    _COUNT

Result is as below.

1.png

Best Regards,
Rico Zhou

 

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

 

AlexisOlson
Super User
Super User

The PATHCONTAINS function should be useful here.

 

See if something like this works:

Count of Transactions =
VAR selPeriod   = SELECTEDVALUE ( 'period table'[period] )
VAR selAccount  = SELECTEDVALUE ( 'account table'[account name] )
VAR subAccounts =
    CALCULATETABLE (
        VALUES ( 'account table'[account name] ),
        FILTER (
            ALL ( 'account table' ),
            PATHCONTAINS ( 'account table'[Path], selAccount )
        )
    )
RETURN
    CALCULATE (
        COUNTROWS ( 'transaction table' ),
        TREATAS ( subAccounts, 'transaction table'[Account Name] )
    )

 

Anonymous
Not applicable

Thank you, @AlexisOlson for your quick reply, but unfortunatelly this also just gives me count only for accounts that actually have the transaction and not the parent accounts.
I just can't wrapp my head around how to make the measure calculate: if a parent account is selected (selection is done from the accounts table side and not the transaction table) sum up all children account transactions.

If the account table is related to the transaction table on [Account Name], then try tweaking the TREATAS in my DAX code to

TREATAS ( subAccounts, 'account table'[Account Name] )

 

Can you share a sample pbix? The way the relationships are set up can make a big difference.

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

FabCon and SQLCon Highlights Carousel

FabCon &SQLCon Highlights

Experience the highlights from FabCon & SQLCon, available live and on-demand starting April 14th.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.