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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request 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
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.