Question on summarizing data/creating table

odonovanc

Board Regular
Joined
Oct 4, 2017
Messages
60
Office Version
  1. 365
I have a large set of data and I am going to provide a summary of it here. It looks something like this starting in A1:

PresidentPhone NumberLocationDateStart Time
George Washington(123)456-7899White House
June 21, 2021​
07:00 AM
George Washington(123)456-7899White House
June 22, 2021​
07:00 PM
George Washington(123)456-7899White House
June 24, 2021​
07:00 AM
George Washington(123)456-7899Pentagon
June 26, 2021​
07:00 PM
George Washington(123)456-7899White House
June 27, 2021​
07:00 AM
John Adams(555) 555-5555Capitol Building
June 25, 2021​
07:00 PM
John Adams(555) 555-5555Capitol Building
June 26, 2021​
07:00 AM
John Adams(555) 555-5555Capitol Building
June 28, 2021​
07:00 PM

I need to summarize these presidents and the location in a format that looks like this:

Phone NumberPresidentLocationDate 1Time 1Date 2Time 2Date 3Time 3Date 4Time 4
(123)456-7899George WashingtonWhite House
June 21, 2021​
June 24, 2021​
07:00 AM​
07:00 PM​
June 24, 2021​
07:00 AM​
June 27, 2021​
07:00 AM​
(123)456-7899George WashingtonPentagon
June 26, 2021​
07:00 PM​
(555) 555-5555John AdamsCapitol Building
June 25, 2021​
07:00 PM​
June 26, 2021​
07:00 AM​
June 28, 2021​
07:00 PM​

So each president and the corresponding location and then the first time they appear there would be date 1, the second time date 2, etc, and corresponding times. Ideally the only blank dates would be at the end if they do not show up. This is a weekly report, so there are never more than 7 dates. Anyone have ideas?

I tried pivoting to summarize, but there are too many columns. Let's assume the bottom set of results, the column phone number is cell A11. If phone number cannot come first, I can move it later. The president's phone numbers will always be the same and unique for each president.
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
So this was an interesting one to solve. Because of your need to pivot/unpivot the data in your columns, I immediately thought of Power Query. However, it's a little more complex then that because we also have to increment the dates/times for each instance of each president. I ended up sharing a little code from here and ended up with the following. It's probably definitely not the most elegant, but it should work. You can clean up things as you like.

If you have your data in an excel table, start a new query from that table and replace what is in the advanced query with this.
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content], //Replace with your table name where the data is coming from
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"President", type text}, {"Phone Number", type text}, {"Location", type text}, {"Date", type text}, {"Start Time", type number}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"President", Order.Ascending}, {"Location", Order.Ascending}}),
    #"Added Index" = Table.AddIndexColumn(#"Sorted Rows", "Index", 0, 1, Int64.Type),
    GroupedRows = Table.Group(#"Added Index", {"President", "Location"}, {{"tmp", each _, type table [President=nullable text, Phone Number=nullable text, Location=nullable text, Date=nullable text, Start Time=nullable number, Index=number]}}),
    #"AddedCustom" = Table.AddColumn(GroupedRows, "Custom", each Table.AddIndexColumn([tmp],"Occurrence", 1,1)),
    #"RemovedOtherColumns" = Table.SelectColumns(AddedCustom,{"Custom"}),
    #"Expanded Custom" = Table.ExpandTableColumn(RemovedOtherColumns, "Custom", {"President", "Phone Number", "Location", "Date", "Start Time", "Index", "Occurrence"}, {"President", "Phone Number", "Location", "Date", "Start Time", "Index", "Occurrence"}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Expanded Custom", {"President", "Phone Number", "Location", "Index", "Occurrence"}, "Attribute", "Value"),
    #"Added Custom" = Table.AddColumn(#"Unpivoted Columns", "Attribute Numbered", each [Attribute] & " " & Text.From([Occurrence])),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index", "Occurrence", "Attribute"}),
    #"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[#"Attribute Numbered"]), "Attribute Numbered", "Value")
in
    #"Pivoted Column"

That gets me this table:
1624651560672.png


You said you might end up with more columns, so run this on your full data source, then you can adjust column types and date/time formatting as appropiate. It's also trivially easy to rearrange the columns in Power Query - simply drag them to where you want them.
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,200
Members
453,022
Latest member
RobertV1609

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top