SOLVED

Pivot Table but Transpose values

Copper Contributor

Hi everyone, 

 

I am trying to figure out a formula/way to quickly transpose all city values from rows to columns for each ID number. As you can tell from below, some ID numbers have multiple cities listed whereas others only have 1-2. Regardless, I want the formula to look at each ID number, see how many cities are listed, and transpose those values into columns. 

 

Screen Shot 2021-05-19 at 10.09.09 AM.png

 

 

Some people have recommended pivot tables but I will show you what happens when I do that. 

Screen Shot 2021-05-19 at 10.14.06 AM.png

Ideal end goal: 

Screen Shot 2021-05-19 at 10.10.28 AM.png

 

Please help!!! 

 

Thank you.

11 Replies

@MasonF2265 

Do you consider Power Query as an option?

@MasonF2265 

With PivotTable

- add data to data model creating PivotTable

- add calculated column Locations as

= VAR tbl = FILTER( 
      ALL('Cities'),
      [ID number] = EARLIER('Cities'[ID number]) &&
      [Citiy] < EARLIER(Cities[Citiy]))
RETURN "Location " & (1+COUNTROWS(tbl) )

- add measure

City Name:=CONCATENATEX(Cities,Cities[City])

- create PivotTable as

image.png

@MasonF2265 

And here is with Power Query

- query the table

- group by Id without aggregation and keeping only lists of cities

- calculate max number of locations and generate list of column names

- extract lists as values and split after that on columns

 

Script is

let
    Source = Excel.CurrentWorkbook(){[Name="Cities"]}[Content],
    #"Grouped Rows" = Table.Group(Source, {"ID number"}, {{"Location", each _[City]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Count", each List.Count([Location])),
    allLocations = {1..List.Max(#"Added Custom"[Count])},
    Columns = List.Transform(allLocations, each "Location " & Text.From(_)),
    #"Extracted Values" = Table.TransformColumns(
        #"Added Custom",
        {"Location", each Text.Combine(List.Transform(_, Text.From), ";"), type text}
    ),
    #"Split Column by Delimiter" = Table.SplitColumn(
        #"Extracted Values",
        "Location", Splitter.SplitTextByDelimiter(";", QuoteStyle.Csv), Columns),
    #"Removed Columns" = Table.RemoveColumns(#"Split Column by Delimiter",{"Count"})
in
    #"Removed Columns"

@MasonF2265 

If with old style formulas

in L5

=IFERROR(
  INDEX(Cities[ID number],
  AGGREGATE(15,6,1/(COUNTIF($L$4:L4, Cities[ID number])=0)*ROW(Cities[ID number]),1) -
  ROW(Cities[[#Headers],[ID number]])),
"")

drag down till empty cells appear

In M5

=IFERROR(
  INDEX(Cities[[City]:[City]],
  AGGREGATE(15,6,1/(Cities[[ID number]:[ID number]]=$L5)*
           (ROW(Cities[[City]:[City]])-ROW(Cities[[#Headers],[City]])),
  COLUMN()-COLUMN($L$4))),
"")

drag down and to the right

@MasonF2265 

If with Excel 365

image.png

in L12

 

=UNIQUE(Cities[ID number])

 

in M12

 

=TRANSPOSE(FILTER(Cities[City],Cities[ID number]=$L12))

 

and drag down

Hi,

Unfortunately I am unable to use Power Query as it appears that it is not available for Mac users. I will try your pivot table recommendation and let you know how that goes. Thanks!

@MasonF2265 

I see. Power Query is available for Mac but as very limited edition. Actually you may only refresh data.

PivotTable as above also won't work on Mac since it requires data model. Mac has no it.

Thus only formulas, one or another variant.

In your old school formulas, I see you have =IFERROR(INDEX(Cities[ID number], AGGREGATE(15,6,1/(COUNTIF($L$4:L5, Cities[ID number])=0)*ROW(Cities[ID number]),1)-ROW(Cities[[#Headers],[ID number]])),"")

When I put this in, it says there is a problem with my formula and points back to the "Cities" you included here. Looking at the spreadsheet you provided, I can only see "City" which is in D3. Are you renaming something or where is the "Cities" part coming in from?
best response confirmed by allyreckerman (Microsoft)
Solution

@MasonF2265 

Cities is the name of this entire structured table

image.png

Do you use range or table? Just in case attached is the latest file.

Hi,

I was FINALLY able to input the formula and have it work correctly! Thank you so so so much!

@MasonF2265 , you are welcome

1 best response

Accepted Solutions
best response confirmed by allyreckerman (Microsoft)
Solution

@MasonF2265 

Cities is the name of this entire structured table

image.png

Do you use range or table? Just in case attached is the latest file.

View solution in original post