Forum Discussion

anjbauer1993's avatar
anjbauer1993
Frequent Visitor
7 years ago
Solved

Unpivoting keys from multiple categories into one column

I am trying to transform an Excel worksheet containing a company's health insurance coverage for its employees. There are three tiers of coverage and four levels of each of those. I was able to transform the insurance dimension without any trouble, it looks like

dim_insurance

With M1, M2, M3 being the three tiers, and a, b, c, d representing who it applies to (Employee, Spouse, etc).

 

Below is a portion of the fact table,

fact

The rows are unique on Employee. As you can see, an employee can have multiple variations of insurance at once. I filtered out null values in M1 - a just to make things easier to visualize. I'm hoping to unpivot the M* - * columns into one long column, with multiple rows for employees that have multiple selections. I'm able to create the same type of key as in the dimension table, but I haven't been able to get them into one column. I have a feeling this involves the unpivot function but I have had no luck so far. Any help or advice is greatly appreciated.

  • I was able to solve this but I didn't end up using unpivot. I converted the values into the respective keys, then added a custom column =  List.Union ( { {col_1}, {col_2}, ... , {col_n} } ) which I then expanded into new rows, giving me the desired result

4 Replies

  • Hi,

     

    Please show your exact expected result.  Also, paste data here such that i can in turn paste that table into an Excel file.

    • anjbauer1993's avatar
      anjbauer1993
      Frequent Visitor

      Dimension:

      DateLevelUserKeyCost
      2018-M1-a519.41
      2018-M1-b519.41
      2018-M1-c289.64
      2018-M1-d519.41
      2018-M2-a474.34
      2018-M2-b474.34
      2018-M2-c264.51
      2018-M2-d474.34
      2018-M3-a623.45
      2018-M3-b623.45
      2018-M3-c347.65
      2018-M3-d623.45

       

       

      Fact Table:

      DateLastFirstM1 - aM1 - bM1 - cM1 - dM2 - aM2 - bM2 - cM2 - dM3 - aM3 - bM3 - cM3 - d
      8/1/2018DoeJohnabcdnullnullnullnullnullnullnullnull
      8/1/2018BauerAndrewanullnullnullnullnullnullnullnullnullnullnull
      8/1/2018DollinDustinnullnullnullnullabcnullanullnullnull

       

       

      And this is the result I am hoping for:

      DateLastFirstInsuranceKey
      8/1/2018DoeJohn2018-M1-a
      8/2/2018DoeJohn2018-M1-b
      8/3/2018DoeJohn2018-M1-c
      8/4/2018DoeJohn2018-M1-d
      8/1/2018BauerAndrew2018-M1-a
      8/1/2018DollinDustin2018-M2-a
      8/2/2018DollinDustin2018-M2-b
      8/3/2018DollinDustin2018-M2-c
  • v-yuta-msft's avatar
    v-yuta-msft
    Icon for Community Support rankCommunity Support

    Hi anjbauer1993,

     



    I'm hoping to unpivot the M* - * columns into one long column, with multiple rows for employees that have multiple selections. I'm able to create the same type of key as in the dimension table, but I haven't been able to get them into one column. 

     

    To be general, in query editor, click on all columns named M*-*, click Transform-> Unpivot Columns and check if it can meet your reuquirement.

     

    Regards,

    Jimmy Tao

  • anjbauer1993's avatar
    anjbauer1993
    Frequent Visitor

    I was able to solve this but I didn't end up using unpivot. I converted the values into the respective keys, then added a custom column =  List.Union ( { {col_1}, {col_2}, ... , {col_n} } ) which I then expanded into new rows, giving me the desired result