Forum Discussion

Gérale-Récolte's avatar
Gérale-Récolte
Frequent Visitor
2 years ago
Solved

Dataflow : recreating a hierarchy from self referencing table

Hi

 

I'm setting up a new dataviz project for my company and I have a use case that I want to submit to this very active and great community 🙂

 

Context : all our data are stored in a postgre db. I connect to this db and load / transform data through a dataflow. Then my users will create reports based on a dataset that will be refreshed by the dataflow I mentionned above.

 

Problem I face : among the tables in the postgre db we have a table that contains the categories for our product. The table is self referencing with a structure like this :

category_idcategory_namecategory_parent_idcategory_parent_name
C001name001nullnull
C002name002C001name001
C003name003C001name001
C004name004C002name002
............

 

This creates a hierarchy of categories in which we publish our products. The hierarchy can be up to 4 levels. For instance :

Fruits & vegetable > Fruits > Apples > Golden

Fruits & vegetable > Fruits > Mango

 

We need this hierarchy in powerBI in order to group metrics per category (for instance, measuring the sales for one level of the category)

 

My first idea for a solution : 

My first guess is to use the transformation capabilities of dataflows to recreate a table that contains the hierarchy :

Level 1Level 2Level 3Level 4category_id
name001nullnullnullC001
name001name002nullnullC002
name001name003nullnullC003
name001name002name004nullC004
...   

 

 

If I can recreate this table, then I should be able to use it in powerBI reports, shouldn't I ?

 

My questions are the following :

  1. Is this the best solution to achieve what I want ? should this transformation be done in the dataflow or elsewhere in powerBI ?
  2. If the dataflow is the right tool to do it, how can I recreate the table from the self referencing category table ?

 

Thanks for your help !

 

Gérald

  • ajohnso2's avatar
    ajohnso2
    2 years ago

    The DAX functions are created specifically for this purpose, for me I would stick with that.

    You could replicate this in your dataflow, or even in your warehouse by creating a flattened hierarchy. If your hierarchy will have the same amount of levels it would make sense to do it at warehouse and store as a table for the sake of using in multiple report solutions.

6 Replies

    • GĂ©rale-RĂ©colte's avatar
      Gérale-Récolte
      Frequent Visitor

      Hi ajohnso2 

       

      Thanks for your reply. I read the document you shared with me. I think DAX could be a good solution but I'm wondering if I'd rather do the transformation in the dataflow itself.

       

      So i tried the following solution :

      1. In power query M, I created a function that parses the entries in the categories db and get all the ancestors for one category. I don't think there is a function similar to the ones in DAX so I had to create one. It's a recursive function that navigates in the hierarchy and collects all the ancestors of one category
      2. I create as many columns as needed and store the values of ancestors in each column

      What do you think of this solution ? Should I keep it or aim for a transformation in the dataset with DAX ?

      I find it hard to decide what's the best implementation.

       

      BR

       

      Gérald

      • ajohnso2's avatar
        ajohnso2
        Solution Supplier

        The DAX functions are created specifically for this purpose, for me I would stick with that.

        You could replicate this in your dataflow, or even in your warehouse by creating a flattened hierarchy. If your hierarchy will have the same amount of levels it would make sense to do it at warehouse and store as a table for the sake of using in multiple report solutions.