Moving date period filter in Excel 2010 pivot-table - excel

Is it possible to set some kind of filter for a moving date period?
For example one of the DB views I'm trying to replicate in my pivot-table has this filter :
DATEDIFF(day, dateColumn, GETDATE()) <= 90
So basically I always want to display the last 90 days of whataver data there is in the cube table.
Is this possible?

The answer to this question is here :
http://blogs.socha.com/2010/05/sliding-date-ranges-with-excel-2010.html
Example for a moving period of 30 days :
Select a cell inside a pivot table bound to the cube so that the PivotTable tools are available
Click the Options tab on the ribbon under the PivotTable Tools section:
Click the Fields, Items & Sets drop-down in the Calculations section of this ribbon tab
Click Manage Sets… in the drop-down
Click New… and then Create Set using MDX…
Enter a name for this set in the Set name text box
Enter the MDX expression that defines the date range
Click OK
Filter(
[Date].[Date].[Date],
[Date].[Date].CurrentMember.Member_Value < Now()
AND [Date].[Date].CurrentMember.Member_Value >= DateAdd("d", -30, VBA![Date]())
)

Related

How to convert Persian (Shamsi) date to Gregorian (Miladi) date with Function Script in Excel or Google Sheet?

Does anyone know how to convert a Persian date to a Gregorian date using an Excel / Google Sheet function?
for example:
1401/06/06
to:
2022/08/28
P.S: Earlier I found a function to convert Gregorian to Jalali written by Amir Fo, But my question is about converting from Persian (Shamsi) to Gregorian.
This can also be accomplished using Power Query, available in Windows Excel 2010+ and Excel 365 (Windows or Mac)
To use Power Query
Select some cell in your Data Table
Data => Get&Transform => from Table/Range or from within sheet
When the PQ Editor opens, over on the right under Applied Steps, the second step will be #"Changed Type"
Edit that step to add fa-IR as the culture
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type date}} , "fa-IR")
This is the same as Changed Type with locale using Persian as the locale
try:
=TEXT(VLOOKUP("Gregorian calendar", IMPORTHTML("https://date-today.com/en/shamsi-"&
REGEXEXTRACT(TO_TEXT(A1), "\/(\d+)")*1&"-"&
REGEXEXTRACT(TO_TEXT(A1), "\/(\d+)\/")*1&"-"&
REGEXEXTRACT(TO_TEXT(A1), "\d{4}")*1&"-to-gregorian-calendar.html", "table", 1), 2, ),
"e/mm/dd")
or:
=TEXT(VLOOKUP("Gregorian calendar", IMPORTHTML("https://date-today.com/en/shamsi-"&
REGEXEXTRACT(TO_TEXT(A1), "\d+")*1&"-"&
REGEXEXTRACT(TO_TEXT(A1), "\/(\d+)\/")*1&"-"&
REGEXEXTRACT(TO_TEXT(A1), "\d{4}")*1&"-to-gregorian-calendar.html", "table", 1), 2, ),
"yyyy/mm/dd")
REVERSE:
Convert date locale in google sheet from Gregorian calendar to Jalali calendar
Here is a pure Excel solution, which does not require an internet connection or Power Query.
Disclaimer: I've found the function here and made some modifications to make it more readable. BTW, I've tested the function for the period 1396/07/01 to 1401/07/01 and it works OK, but use it with extensive testing and at your own risk.
Basically, the function calculates the total days since 1278/10/11 Shamsi (which is equal to the Excel date origin, 1900-01-01). The result of the function is an integer (date value).
Below, you will find the solution in two formats, one using the new LET function (for Excel 2021 and Office 365) and one without:
Note that the Shamsi date should be in the format 1401/07/21.
Using LET:
=LET(
y,VALUE(LEFT(A1,4)),
m,VALUE(MID(A1,6,2)),
d,VALUE(RIGHT(A1,2)),
full_months,IF((m-1)<7,(m-1)*31,IF((m-1)>6,(m-1)*30+6)),
total,(y-1)*365+full_months+d+INT((y-1)/4),
IF(MOD(y,4)=0,total+1,total)-466710
)
Legacy version:
IF(
MOD(VALUE(LEFT(A1,4)),4)=0,
(VALUE(LEFT(A1,4))-1)*365+(
IF(
(VALUE(MID(A1,6,2))-1)<7,
(VALUE(MID(A1,6,2))-1)*31,
IF(
(VALUE(MID(A1,6,2))-1)>6,
(VALUE(MID(A1,6,2))-1)*30+6
)
)
)+VALUE(RIGHT(A1,2))+INT((VALUE(LEFT(A1,4))-1)/4)+1,
(VALUE(LEFT(A1,4))-1)*365+(
IF(
(VALUE(MID(A1,6,2))-1)<7,
(VALUE(MID(A1,6,2))-1)*31,
IF(
(VALUE(MID(A1,6,2))-1)>6,
(VALUE(MID(A1,6,2))-1)*30+6
)
)
)+VALUE(RIGHT(A1,2))+INT((VALUE(LEFT(A1,4))-1)/4)
)-466710
To convert the resulting integer to date, either change the cell format to Date or use the following formula:
TEXT(A2,"yyyy-mm-dd")
Microsoft Excel supports all calendar conversion.
The key is to follow procedures one by one.
suppose We want to convert cell A1=10/10/1401 from Persian calendar to English Calendar and set the result in the Cell B1
Step 1:
Right Click on B1 > Format cells... from Number tab in the category select Date and change it to desire calendar type, in this case English.
For B1 also type the formula =A1.
Step 2:
Right click on A1 > Format cells... from Number tab in the category select Date and change the calendar type to whatever your date type is; in this case Persian.
Tick the check mark input dates according to selected calendar
Step 3:
Enter your Persian date in cell A1. You'll get the converting date in cell B1.
Be aware of the order you enter the year and month and the day.

Excel Pivot - Calculate average for values > XX

Data table simplified - read only:
<table border=1>
<tr><th>Date</th><th>Hours</th></tr>
<tr><td>Jan. 1</td><td>6.5</td></tr>
<tr><td>Jan. 2</td><td>8.5</td></tr>
<tr><td>Jan. 3</td><td>7.5</td></tr>
<tr><td>Jan. 4</td><td>9.0</td></tr>
</table>
Now I would like a pivot table that can calculate the average number of hours - only taking into account the days where the number of hours are > 8.0. Ie. for the above data the pivot should return 8.75.
I've tried with calculated field: =If(Hours > 8, Hours, NULL) - or blanks instead of null or similar.
Please help.
You don't need a Pivot table. Just use =AverageIf([Range of Hours],">8")
Edit:
To achieve through Pivot Table, I can only think of using "measure". But it could be quite slow for 120k rows as the Filter() function need to go through each row one by one.
create a the Pivot Table with the checkbox "Add this data to the Data Model" clicked;
Right-click on the "Range" (or the table name if your data is a table) in the Field List and choose "Add Measure"
Enter the measure's info and use =SUMX(Range, if(Range[Hour]>8, Range[Hour],0))/ COUNTROWS(FILTER(ALL(Range), Range[Hour]>8)) as formula (replace "Range" with Table name if source data is a table). And click "OK".
Then, you can use this measure to get the average value you want.

Excel Sum by Month

Hi have the following scenario:
Items in Column A, Total Time in Column B, then I have 365 dates in columns.
I want to summarise the column dates by month.
I want, for example, for Item A, in March, total Times taken.
How to do it?
Thanks
So, somewhat overkill but was interested in how to do it with power query and was interested in building a custom function for the MonthNumber.
Powerquery is free add-in for pre 2016 and is inbuilt for 2016
STEPS:
1) Data tab (2016) or powerquery tab (2010 to 2016) => Select query from table and ensure your source data is selected and appears in pop-up
2) Select the first date column, hold shift down and select last date, then select unpivot columns
3) Make sure column Attribute (your pivoted dates) is formatted as a date column
4) Open the advanced query editor window
and paste the following between let and source
fnMonthNum = (input) => let
values = {
{"January", "1"},
{"February", "2"},
{"March", "3"},
{"April","4"},
{"May", "5"},
{"June", "6"},
{"July", "7"},
{"August", "8"},
{"September", "9"},
{"October", "10"},
{"November", "11"},
{"December", "12"},
{input, "Undefined"}
},
Result = List.First(List.Select(values, each _{0}=input)){1}
in
Result,
This is M code for creating a custom function that returns month numbers from month names.
5) Add column tab => Add custom column, rename it MonthName and insert the following text
=Date.ToText([Attribute],"MMMM")
6) Add another custom column, rename it MonthNum and use our new function here:
= fnMonthNum([MonthName])
7) Remove the now unnecessary Attribute column; select column => right click => remove
8) Transform tab => Group by and enter the following:
9) Make sure MonthNum column is numeric.
10) Close and load to => Only create connection,
Tick checkbox for Add to data model, and then Load
11) As per instructions here: CREATE A PIVOT TABLE USING EXCEL’S INTERNAL DATA MODEL
Choose Insert→PivotTable from the Ribbon. The Create PivotTable dialog box opens.
Select the Use an External Data Source option, as shown, and then click the Choose Connection button. You see the Existing Connections dialog box, as shown.
Select the query you just created for Table1 and a destination for the pivottable
12) Arrange the fields as required and sort ascending on the MonthNum rowfield
13) Insert slicer
Note: You could introduce a custom sort to avoid having to use the MonthNum column for sorting.

How get rows with oldest date per year

I have task in excel. I think I show you it on example. Let say we have table as:
ID date
1 2015-03-11
1 2015-05-13
2 2013-01-10
2 2010-05-11
1 2014-09-19
2 2013-04-01
I have to make some operations to get rows with oldest date per every year. So I should have:
ID date
1 2015-03-11
1 2014-09-19
2 2013-01-10
2 2010-05-11
I will grateful for any help. Thanks in advance!
This is but one option. I like using SQL for this type of work and since Excel can connect to itself as an ODBC data source, that's just what I did here...
Create a Named range in excel (I called mine SomeTable) I do this by selecting the range in question and clicking in the drop down field to the left of the formula space that usually lists the selected cell (B11 in image below)
I then select data, from external sources and select the option for Microsoft Query (ODBC). Select new data source give it a name (Excel File name) Select microsoft excel driver. click connect. browse to where the file is containing the named range (Some table) Select ok and then in the 4th option select the named range (SomeTable)... select a place to put the table on a worksheet.
Now click in the "table" data it creates and go to the data menu properties. and enter the following in the definition tab under command text
.
Select ID, Date
FROM SomeTable ST
INNER JOIN
(Select MIN(date) as mDate, year(date) as mYear
FROM someTable
Group by year(date)) A on
ST.Date = A.mDate
If all done correctly you should get results like this:
Column EF is the source table named "SomeTable"
A10 is where I chose to put the table
B20 is where the SQL used to get the max per year
was put.

Can Pivot Table perform operations comparing two field?

I have a table that has two category columns, a column for number started and a column for number finished. I'd like to roll up by one category column and calculate a percentage finished in a pivot table (rather than array formulae).
Imagine my data looks like this:
Date - School - Starters - Finishers
7/1 USC 100 25
7/1 UCLA 200 50
7/2 USC 250 100
7/2 UCLA 200 100
I want my pivot table to look like this
Date - Finish rate
7/1 25%
7/2 44%
But when I go into Value Field Settings to switch to a calculation, I can't figure how to get the column comparison to happen.
You can create a calculated field that compares two other fields in the "Pivottable Tools" "Analyze" ribbon "Fields, Items, & Sets" button - "Calculated Field". Just build comparison expression in the "Formula" text field of the "Insert Calculated Field" dialogue box.

Resources