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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
Bassant_Saied
Microsoft Employee
Microsoft Employee

Calculate tasks in a store & its descendant departments

Hi,

 

I have two tables one contains tasks & other has store & departments linked by parent_id column

for example:

Store table:

NameIDParent IDType
Store 11 Store
Department A21Department
Department B31Department

Task Table:

NameIDOwningStoreIDStatus
Task A11Open
Task B22Completed
Task C33Open

I want to create a measure to count all tasks opened for a store & all tasks open in the descendant departments. Which means to show count 3 for store 1

 

How could I do this? Thanks! 

1 ACCEPTED SOLUTION
ERD
Community Champion
Community Champion

Hi @Bassant_Saied , one of the ways is to use hierarchies. For that a number of calculated columns and measures need to be created.

Calculated columns in the Store table:

FullPath = PATH( Store[ID], Store[Parent ID])
Level1 = LOOKUPVALUE(Store[Name], Store[ID], PATHITEM(Store[FullPath], 1, INTEGER))
Level2 = IF(PATHLENGTH(Store[FullPath]) >= 2, LOOKUPVALUE(Store[Name], Store[ID], PATHITEM(Store[FullPath], 2, INTEGER)), Store[Level1])
NodeDepth = PATHLENGTH(Store[FullPath])
IsLeaf =
VAR c_id = Store[ID]
VAR depsAtParentLevel = CALCULATE ( COUNTROWS ( Store ), ALL ( Store ), Store[Parent ID] = c_id )
RETURN
    depsAtParentLevel = 0

Measures:

tasks = COUNTROWS(Task)
Tasks amt =
VAR check = MAX ( Store[NodeDepth] ) + 1 < [BrowseDepth]
VAR extraLevel = MAX ( Store[NodeDepth] ) + 1 = [BrowseDepth]
VAR amt = [tasks]
VAR hasData = NOT ISBLANK ( amt )
VAR leaf = SELECTEDVALUE ( Store[IsLeaf], FALSE () )
VAR result = IF ( NOT check, IF ( extraLevel, IF ( NOT leaf && hasData, amt ), amt ) )
RETURN
    IF ( MAX ( Store[NodeDepth] ) < [BrowseDepth], BLANK (), result )

ERD_0-1694002703468.png

If you want to show the amount of tasks per store itself as well:

Tasks amt =
VAR check = MAX ( Store[NodeDepth] ) + 1 < [BrowseDepth]
VAR extraLevel = MAX ( Store[NodeDepth] ) + 1 = [BrowseDepth]
VAR amt = [tasks]
VAR hasData = NOT ISBLANK ( amt )
VAR leaf = SELECTEDVALUE ( Store[IsLeaf], FALSE () )
VAR result = IF ( NOT check, IF ( extraLevel, IF ( NOT leaf && hasData, amt ), amt ) )
RETURN
    result 

ERD_1-1694002777305.png

 

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

Check out my latest demo report in the data story gallery.

Stand with Ukraine!


Here are official ways you can support Ukraine financially (accounts with multiple currencies):
1) Support the Armed Forces of Ukraine: https://bank.gov.ua/ua/about/support-the-armed-forces
2) Come Back Alive foundation: https://www.comebackalive.in.ua/

Thank you!

View solution in original post

2 REPLIES 2
Bassant_Saied
Microsoft Employee
Microsoft Employee

Thanks @ERD  for your solution.

Actually there is also another simpler solution using Path functions. I created a ne column called PathStore with has the Path value for each store & his parents. My problem was whenever store is filtered the count of tasks rows are filtered too. So I use this formula:

VAR SelectedStore = SELECTEDVALUE ( task[owningStoreID] )

VAR
TotalTasks = CALCULATE (
     COUNTROWS(task),
     FILTER(ALL(task),
        PATHCONTAINS (task[PathStore], SelectedBU)
)
)
ERD
Community Champion
Community Champion

Hi @Bassant_Saied , one of the ways is to use hierarchies. For that a number of calculated columns and measures need to be created.

Calculated columns in the Store table:

FullPath = PATH( Store[ID], Store[Parent ID])
Level1 = LOOKUPVALUE(Store[Name], Store[ID], PATHITEM(Store[FullPath], 1, INTEGER))
Level2 = IF(PATHLENGTH(Store[FullPath]) >= 2, LOOKUPVALUE(Store[Name], Store[ID], PATHITEM(Store[FullPath], 2, INTEGER)), Store[Level1])
NodeDepth = PATHLENGTH(Store[FullPath])
IsLeaf =
VAR c_id = Store[ID]
VAR depsAtParentLevel = CALCULATE ( COUNTROWS ( Store ), ALL ( Store ), Store[Parent ID] = c_id )
RETURN
    depsAtParentLevel = 0

Measures:

tasks = COUNTROWS(Task)
Tasks amt =
VAR check = MAX ( Store[NodeDepth] ) + 1 < [BrowseDepth]
VAR extraLevel = MAX ( Store[NodeDepth] ) + 1 = [BrowseDepth]
VAR amt = [tasks]
VAR hasData = NOT ISBLANK ( amt )
VAR leaf = SELECTEDVALUE ( Store[IsLeaf], FALSE () )
VAR result = IF ( NOT check, IF ( extraLevel, IF ( NOT leaf && hasData, amt ), amt ) )
RETURN
    IF ( MAX ( Store[NodeDepth] ) < [BrowseDepth], BLANK (), result )

ERD_0-1694002703468.png

If you want to show the amount of tasks per store itself as well:

Tasks amt =
VAR check = MAX ( Store[NodeDepth] ) + 1 < [BrowseDepth]
VAR extraLevel = MAX ( Store[NodeDepth] ) + 1 = [BrowseDepth]
VAR amt = [tasks]
VAR hasData = NOT ISBLANK ( amt )
VAR leaf = SELECTEDVALUE ( Store[IsLeaf], FALSE () )
VAR result = IF ( NOT check, IF ( extraLevel, IF ( NOT leaf && hasData, amt ), amt ) )
RETURN
    result 

ERD_1-1694002777305.png

 

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

Check out my latest demo report in the data story gallery.

Stand with Ukraine!


Here are official ways you can support Ukraine financially (accounts with multiple currencies):
1) Support the Armed Forces of Ukraine: https://bank.gov.ua/ua/about/support-the-armed-forces
2) Come Back Alive foundation: https://www.comebackalive.in.ua/

Thank you!

Helpful resources

Announcements
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 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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