我有一个数据如下。真实数据要大得多,列也更多。这是酒店预订记录。
我希望对其进行汇总,以显示每天有多少种类型的房间被预订,输出如下:
对于如此小的数据,Power Query 中的以下代码运行良好。然而,对于大约有16k行的真实数据,加载数据需要半个多小时。
let
Source = List.Dates(Date.From(List.Min(Bookings[Arrival Date])),Duration.Days(DateTime.Date(DateTime.LocalNow()) - Date.From(List.Min(Bookings[Arrival Date]))) + 1,#duration(1,0,0,0)),
#"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
#"Renamed Columns" = Table.RenameColumns(#"Converted to Table",{{"Column1", "Date"}}),
#"Changed Type" = Table.TransformColumnTypes(#"Renamed Columns",{{"Date", type date}}),
#"Added Standard" = Table.AddColumn(#"Changed Type", "Standard", each List.Count(
Table.SelectRows(
Bookings,
(Standard) => Standard[Arrival Date] <= [Date]
and
Standard[Departure Date]>[Date]
and
Standard[Room Type]="Standard"
)[Booking Number]
)),
#"Added Deluxe" = Table.AddColumn(#"Added Standard", "Deluxe", each List.Count(
Table.SelectRows(
Bookings,
(Deluxe) => Deluxe[Arrival Date] <= [Date]
and
Deluxe[Departure Date]>[Date]
and
Deluxe[Room Type]="Deluxe"
)[Booking Number]
)),
#"Added Suite" = Table.AddColumn(#"Added Deluxe", "Suite", each List.Count(
Table.SelectRows(
Bookings,
(Suite) => Suite[Arrival Date] <= [Date]
and
Suite[Departure Date]>[Date]
and
Suite[Room Type]="Suite"
)[Booking Number]
)),
#"Changed to Number" = Table.TransformColumnTypes(#"Added Suite",{{"Standard", Int64.Type}, {"Deluxe", Int64.Type}, {"Suite", Int64.Type}})
in
#"Changed to Number"
还有其他适用于大数据的解决方案吗?
Table.SelectRows
相对较慢。
看看这是否能更快地发挥作用。如果是的话,那么很容易做出任何需要的细微调整。
let
Source = Excel.CurrentWorkbook(){[Name="Bookings"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{
{"Booking Number", Int64.Type}, {"Arrival Date", type date}, {"Departure Date", type date}, {"Room Type", type text}}),
//add column with all of the dates for each booking
//This will include both the arrival and departure dates
//easy to adjust if one should be excluded
#"All Dates" = Table.AddColumn(#"Changed Type","allDates",
each List.Dates([Arrival Date],
Duration.Days([Departure Date]-[Arrival Date])+1,
#duration(1,0,0,0)), type {date}),
//expand to one entry poer date
#"Expanded allDates" = Table.ExpandListColumn(#"All Dates", "allDates"),
//Some entries are not capitalized
//may need some other adjustments depending on real data
#"Capitalized Each Word" = Table.TransformColumns(#"Expanded allDates",{{"Room Type", Text.Proper, type text}}),
//Group by dates and room type
#"Grouped Rows" = Table.Group(#"Capitalized Each Word", {"allDates", "Room Type"}, {{"Count", each Table.RowCount(_), Int64.Type}}),
//then pivot
#"Pivoted Column" = Table.Pivot(#"Grouped Rows", List.Distinct(#"Grouped Rows"[#"Room Type"]), "Room Type", "Count", List.Sum)
in
#"Pivoted Column"