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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
MaximeG
Frequent Visitor

Transform specific numbers into rows

Hello,

 

In order of making data viable for analysis, I have to transform data coming from a singular value to multiple rows.

For example:

 

Country - SampleSize - Vaccinated

{Belgium - 5 - 2 , 

France - 3 - 1}

This data should become as following:

 

Belgium - Vaccinated

Belgium - Vaccinated

Belgium - Not-vaccinated

Belgium - Not-vaccinated

Belgium - Not-vaccinated

France - Vaccinated

France - Not-vaccinated

France - Not-vaccinated

 

I have been trying to make a for-loop function with SampleSize as the 'i', but I can't wonder if there isn't a more efficient way of doing so.

 

Hope you can help me and thanks!

 

 

1 ACCEPTED SOLUTION
BA_Pete
Super User
Super User

Hi @MaximeG ,

 

In Power Query, go to New Source>Blank Query then in Advanced Editor paste my code over the default code. You can then follow the steps I took to complete this:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSkrNSc8szVXSUTIFYiOlWJ1opbSixLzkVCDXGIgNlWJjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [country = _t, sample = _t, vaccinated = _t]),
    chgTypes = Table.TransformColumnTypes(Source,{{"country", type text}, {"sample", Int64.Type}, {"vaccinated", Int64.Type}}),
    addUnvaccinated = Table.AddColumn(chgTypes, "unvaccinated", each [sample] - [vaccinated]),
    unpivotOthCols = Table.UnpivotOtherColumns(addUnvaccinated, {"country", "sample"}, "Attribute", "Value"),
    addList = Table.AddColumn(unpivotOthCols, "list", each {1..[Value]}),
    expandList = Table.ExpandListColumn(addList, "list"),
    remOthCols = Table.SelectColumns(expandList,{"country", "Attribute"})
in
    remOthCols

 

SUMMARY:

1) Add column with number of unvaccinated.

2) Unpivot [vaccinated] and [unvaccinated] columns

3) Add column creating list between 1 and vax/unvax value.

4) Expand list to duplicate rows.

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




View solution in original post

2 REPLIES 2
MaximeG
Frequent Visitor

Thank you very much for your help Pete!

The solution was very understandable and works perfectly.

BA_Pete
Super User
Super User

Hi @MaximeG ,

 

In Power Query, go to New Source>Blank Query then in Advanced Editor paste my code over the default code. You can then follow the steps I took to complete this:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSkrNSc8szVXSUTIFYiOlWJ1opbSixLzkVCDXGIgNlWJjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [country = _t, sample = _t, vaccinated = _t]),
    chgTypes = Table.TransformColumnTypes(Source,{{"country", type text}, {"sample", Int64.Type}, {"vaccinated", Int64.Type}}),
    addUnvaccinated = Table.AddColumn(chgTypes, "unvaccinated", each [sample] - [vaccinated]),
    unpivotOthCols = Table.UnpivotOtherColumns(addUnvaccinated, {"country", "sample"}, "Attribute", "Value"),
    addList = Table.AddColumn(unpivotOthCols, "list", each {1..[Value]}),
    expandList = Table.ExpandListColumn(addList, "list"),
    remOthCols = Table.SelectColumns(expandList,{"country", "Attribute"})
in
    remOthCols

 

SUMMARY:

1) Add column with number of unvaccinated.

2) Unpivot [vaccinated] and [unvaccinated] columns

3) Add column creating list between 1 and vax/unvax value.

4) Expand list to duplicate rows.

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Top Solution Authors