March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early bird discount ends December 31.
Register NowBe 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
Hi,
I have two tables one contains tasks & other has store & departments linked by parent_id column
for example:
Store table:
Name | ID | Parent ID | Type |
Store 1 | 1 | Store | |
Department A | 2 | 1 | Department |
Department B | 3 | 1 | Department |
Task Table:
Name | ID | OwningStoreID | Status |
Task A | 1 | 1 | Open |
Task B | 2 | 2 | Completed |
Task C | 3 | 3 | Open |
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!
Solved! Go to Solution.
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 )
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
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!
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)
)
)
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 )
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
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!
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!
Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.
User | Count |
---|---|
21 | |
14 | |
11 | |
7 | |
5 |
User | Count |
---|---|
28 | |
21 | |
20 | |
13 | |
10 |