Auto select latest 4 dates in Pivot - Column Filter

shansakhi

Active Member
Joined
Apr 5, 2008
Messages
276
Office Version
  1. 365
Platform
  1. Windows
Hello Everybody,
I am working on a huge data which has multiple dates and I want to display data only for latest 4 dates.
How can I Auto select latest 4 dates in Pivot - Column Filter.

Regards,
Shan
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Show us some sample data 8-10 records using XL2BB. Do not post pictures as we can not manipulate data in pictures.
 
Upvote 0
Thank you for your email.
Please find the data for your reference. Here I would like to see only 4 latest As of Dates in pivot.

Book1
ABCDEFGHIJK
1MonthAs of Date Capacity
2Jan8-Dec654,541Sum of CapacityAs of Date
3Jan1-Dec624,409Month8-Dec1-Dec24-Nov17-NovGrand Total
4Jan24-Nov651,927Jan6545416244096519276261092556986
5Jan17-Nov626,109Feb5910935637155883395656052308752
6Jan10-Nov603,571Mar6565686301456559546359322578599
7Feb8-Dec591,093Grand Total19022021818269189622018276467444337
8Feb1-Dec563,715
9Feb24-Nov588,339
10Feb17-Nov565,605
11Feb10-Nov533,425
12Mar8-Dec656,568
13Mar1-Dec630,145
14Mar24-Nov655,954
15Mar17-Nov635,932
16Mar10-Nov601,829
17
18
Sheet1
 
Upvote 0
Use Power Query to filter your dates and then pivot the results.

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"As of Date", type date}}),
    #"Filtered Rows" = Table.SelectRows(#"Changed Type", each ([As of Date] <> #date(2022, 11, 10))),
    #"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Filtered Rows", {{"As of Date", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(#"Filtered Rows", {{"As of Date", type text}}, "en-US")[#"As of Date"]), "As of Date", " Capacity")
in
    #"Pivoted Column"

Book5
ABCDEFGHIJK
1MonthAs of Date Capacity
2Jan12/8/2022654541Sum of CapacityAs of Date
3Jan12/1/2022624409Month12/8/202212/1/202211/24/202211/17/2022Grand Total
4Jan11/24/2022651927Jan6545416244096519276261092556986
5Jan11/17/2022626109Feb5910935637155883395656052308752
6Jan11/10/2022603571Mar6565686301456559546359322578599
7Feb12/8/2022591093Grand Total19022021818269189622018276467444337
8Feb12/1/2022563715
9Feb11/24/2022588339Month12/8/202212/1/202211/24/202211/17/2022
10Feb11/17/2022565605Feb591093563715588339565605
11Feb11/10/2022533425Jan654541624409651927626109
12Mar12/8/2022656568Mar656568630145655954635932
13Mar12/1/2022630145
14Mar11/24/2022655954
15Mar11/17/2022635932
16Mar11/10/2022601829
17
Sheet1
 
Upvote 0
Given your statement that you "working on a huge data", I agree with Alan that using Power Query would be the way to go.

I think to make it flexible it will need a few more steps than Alan has.

Much as I don't like using the default table name I have assumed your initial data is called "Table1" in the below.

1) Create a new query (blank or just load the data) and give it the name it 01_Data (in the panel on the right of the PQ editor)
The source line of this is the only reference to "Table1" if you need to chang the table name.

a) In the advanced editor. Replace everything your see there with the below:
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    ChangedTypeDateOnly = Table.TransformColumnTypes(Source,{{"As of Date", type date}})
in
    ChangedTypeDateOnly

b) Close and Load as connection only

2)
Create a second blank query (either a blank query or simply duplicate the above) I have called it "02_Last_n_Dates" but the name doesn't matter.
(This references 01_Data)
In the below the number 4 for your last dates appears in the step "FilteredLast_n_Dates"

a) In the advanced editor. Replace everything you see there with the below:
Power Query:
let
    Source = #"01_Data",
    #"Removed Other Columns" = Table.SelectColumns(Source,{"As of Date"}),
    #"Sorted Rows" = Table.Sort(#"Removed Other Columns",{{"As of Date", Order.Descending}}),
    #"Removed Duplicates" = Table.Distinct(#"Sorted Rows"),
    #"Added Index" = Table.AddIndexColumn(#"Removed Duplicates", "Index", 1, 1, Int64.Type),
    FilteredLast_n_Dates = Table.SelectRows(#"Added Index", each [Index] <= 4),
    MergedQueriesDateWithData = Table.NestedJoin(FilteredLast_n_Dates, {"As of Date"}, #"01_Data", {"As of Date"}, "01_Data", JoinKind.LeftOuter),
    #"Expanded 01_Data" = Table.ExpandTableColumn(MergedQueriesDateWithData, "01_Data", {"Month", " Capacity"}, {"Month", " Capacity"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded 01_Data",{"Index"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Removed Columns",{{"Month", type text}, {" Capacity", Int64.Type}})
in
    #"Changed Type"

b) Close and Load To > pick somewhere on your workbook or let it create a new Sheet.

This will create a new Table with just the last 4 days you can use that for your pivot. You also have the option of continuing on in PQ and pivoting the data there.
There is also the option of putting all your data into the data model.

My output:
(it should appear in your date format when you run it)

20221221 PQ Last 4 Dates shansakhi.xlsm
LMN
2As of DateMonth Capacity
38/12/2022Mar656568
48/12/2022Feb591093
58/12/2022Jan654541
61/12/2022Mar630145
71/12/2022Feb563715
81/12/2022Jan624409
924/11/2022Mar655954
1024/11/2022Jan651927
1124/11/2022Feb588339
1217/11/2022Mar635932
1317/11/2022Feb565605
1417/11/2022Jan626109
Data
 
Last edited:
Upvote 0
Thank you for your reply.
But I want to use this option of latest 4 dates in pivot.
My data is loaded as connection with pivot.
 
Upvote 0
The data is already loaded in Power BI and a pivot is created as connection.
The latest 4 dates I want to show in Pivot only.
 
Upvote 0
In that case your best bet is to add a field in Power BI that identifies the last 4 dates and use that as your filter in the Excel Pivot Table.
 
Upvote 0

Forum statistics

Threads
1,215,212
Messages
6,123,653
Members
449,111
Latest member
ghennedy

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