Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.
Hello everyone,
I am trying to set up a parent child hierarchy from a flat file I have received. The flat file does not distinguish between hierarchy levels and does not have a simple way to form those levels, however, there is SOME logic to it which I would like to extract either via M or DAX. The process should be automated so that updated versions of the flat file will get their hierarchy created upon refresh.
The flat file looks like this (numbers are of course dummy values):
Product, 1st Quarter, 2nd Quarter, 3rd Quarter, 4th Quarter (later added index column)
"A", 1000, 1000, 1000, 1000 (1)
"A B", 1000, 1000, 1000, 1000 (2)
"A B C", 1000, 1000, 1000, 1000 (3)
"A B C D", 1000, 1000, 1000, 1000 (4)
"A B C D E", 1000, 1000, 1000, 1000 (5)
"A B C E", 1000, 1000, 1000, 1000 (6)
"A B C E F", 1000, 1000, 1000, 1000 (7)
"A B C D G", 1000, 1000, 1000, 1000 (8)
The longer the name, the lower in the hierarchy. The higher level is always contained within the name of the lower level, however, each letter in the example can be several words long (so "IAMJUSTONEWORD" can be the same level as "I AM MORE THAN ONE WORD"). And if you look closely at the last example row (8), it is not a child of the higher level before (6) but actually belongs to A B C D (4). While a higher level will always come before the lower level (A B C D will never be in a row before A B C), they are not directly after one another.
I have added an index column to every row and I found ways to extract an exact number of rows prior but as I don't know how many rows up the higher level is, this does not help. I actually need to look for the name of previous rows and get the highest index of a row contained within the name of the current row. So for row "A B C D" (4) I need the index of the row "A B C" (3), even though the rows "A" (1) and "A B" (2) are also contained within (as they are higher levels of the same hierarchy).
I have - with some hard-coding for the categories (which is acceptable as they won't change too often although it would be preferable to get them dynamically) - set up a calculated column to give me the hierarchy level. There are always 5 levels (0-4), level 0 and level 1 are just checking if the product name is in a certain list, level 3 always ends in " G" (and no other row ever will), level 2 always comes before a level 3 line and level 4 is all the rest. This is the DAX code I use:
Solved! Go to Solution.
Hi @yamayancha
You can create a calculated column with below DAX code to get its higher level's index for each row.
Column =
MAXX (
FILTER (
'FLATFILE (2)',
SEARCH ( 'FLATFILE (2)'[DESC], EARLIER ( 'FLATFILE (2)'[DESC] ),, 0 ) > 0
&& 'FLATFILE (2)'[Index] < EARLIER ( 'FLATFILE (2)'[Index] )
),
'FLATFILE (2)'[Index]
)
Note that SEARCH is case-insensitive. You could also try FIND which is case-sensitive.
Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.
Hi @yamayancha
You can create a calculated column with below DAX code to get its higher level's index for each row.
Column =
MAXX (
FILTER (
'FLATFILE (2)',
SEARCH ( 'FLATFILE (2)'[DESC], EARLIER ( 'FLATFILE (2)'[DESC] ),, 0 ) > 0
&& 'FLATFILE (2)'[Index] < EARLIER ( 'FLATFILE (2)'[Index] )
),
'FLATFILE (2)'[Index]
)
Note that SEARCH is case-insensitive. You could also try FIND which is case-sensitive.
Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.
Thank you, this was exactly what I was looking for!
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
108 | |
100 | |
78 | |
64 | |
58 |
User | Count |
---|---|
148 | |
111 | |
94 | |
84 | |
67 |