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.
Related
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.
I'm struggling with probably the simplest thing. How do I filter my data using a where clause in Excel 2016, ODBC sql database source.
I'm new to Excel 2016, I used to just modify the SQL Command via the odbc properties. Now i can't even see the SQL command when I create the odbc.
So from just playing around. I gathered that there is now a series of "Applied Steps", but I struggle to find where I can find how to limit the rows to a date range. (I don't want to display the date because I'm aggregating by inventory type) But there must be somewhere I can say return only for date '2020-05-01' for example. (where date = '2020-05-01')
the next steps will then be making that a dynamic parameter so I can have some sort of input when the data refresh.
Any tips?
The SQL statement is greyed out
Right clicking a step -> view native SQL will gray out when that step cannot be query folded. Previous steps are still viewable.
You can find the sql query and server name if you choose advanced editor -- which displays all steps in one editor.
native sql
If you want raw sql, import the table as a new connection and paste your query into that box.
note: [] in powerquery is a Record literal.
// indented for readability
let
Source = Sql.Database("server", "dbo"),
// using parameterized queries
RawSQL = "SELECT * FROM users as
WHERE EnglishMonthName=#MonthName
AND
EnglishDayNameOfWeek=#DayName",
TableName = Value.NativeQuery(
Source, RawSQL
[
MonthName="March",
DayName="Tuesday"
]
)
in
TableName
I don't want to display the date because I'm aggregating by inventory type
Using the UI, you would import the table keeping the date column. Filter rows. Then choose remove column as your last step. Your model will not display removed columns.
Where clause in PowerQuery aka: Get Data
Where is the function Table.SelectRows The UI will generate the code for you
Right click your date column -> filter -> date/time -> equals
If you want 2020/05/09 It generates:
= Table.SelectRows(#"Source Table", each [Date] = #date(2020, 5, 9))
You may use multiple conditions, like a date range
= Table.SelectRows(#"Table Name", each
[Date] >= #date(2020, 5, 9) and
[Date] <= #date(2020, 6, 1)
)
I have 2 large tables in power pivot and I am trying to reconcile stockpile build grades to crushed stockpile grades. Please see example. I can create pivot table that contains the crushed grades but I am unable to find the right way to bring the stockpile grades though for the reconciliation high lighted in green in the attached example.
Thanks for any help or direction on where to look
In Power Query, create your lookup tables.
1) unique crushers, ID
2) Dates, ID
Here is a function to create a dates table, if you need one. After you invoke the function to get the column of dates, add another column for the ID.
/*--------------------------------------------------------------------------------------------------------------------
PQ Create a Dates Table, returning a single column of dates.
Inputs:
Start Date | Enter the year as yyyy, month as mm, day as dd
End Date | Enter the year as yyyy, month as mm, day as dd
Increments | One row will be returned per increment.
Author: Jenn Ratten
Edits:
07/16/18 | Modified query copied from the internet.
10/01/19 | Converted to a function.
--------------------------------------------------------------------------------------------------------------------*/
let
fDatesTable = (StartYear as number, StartMonth as number, StartDay as number, EndYear as number, EndMonth as number, EndDay as number, IncrementDays as number, IncrementHours as number, IncrementMin as number, IncrementSec as number) as table =>
let
StartDate = #date(StartYear,StartMonth,StartDay),
EndDate = #date(EndYear,EndMonth,EndDay),
Increments = #duration(IncrementDays,IncrementHours,IncrementMin,IncrementSec),
DatesTable = Table.FromColumns({List.Dates(StartDate, Number.From(EndDate) - Number.From(StartDate), Increments)}, type table[Date]),
ChangeType = Table.TransformColumnTypes(DatesTable,{{"Date", type date}})
in
ChangeType
in
fDatesTable
Load all of the tables to the data model.
Go to Power Pivot, diagram view, and create your relationships.
Lookup Crusher to data tables 1 and 2
Lookup Date to data tables 1 and 2
Go to Data View on data tables 1 and 2, add 2 new columns for the lookup IDs. You can specify the column header and the formula at one time by clicking in first cell and using this syntax, then either press enter or click the check mark in the formula bar.
Dates Lookup ID:=RELATED(lookup_dates[ID])
Crusher Lookup ID:=RELATED(lookup_crusher[ID])
Optional, but a good practice....
Right-click the new fields you just created and select "hide from client tools". Also hide the date and crusher fields on both data tables, and the ID field on both lookup tables. When you are creating pivots to summarize data from more than one table, the text fields that you place on your pivot table should be the fields that are shared (aka the lookup tables). This helps to minimize pivots in which the grand totals don't match the sum that you actually see on the table. If you hide the fields, it reminds you of that. There are exceptions of course, but this is a good rule of thumb.
Now create measures to sum the tons and any other math calculations you'd like. With the measures, start simple and let the pivot do the slicing. Put the measures in the values section of the pivot table.
Sum of Source Tons:=sum(Table1[Tons])
Sum of Destination Tons:=sum(Table2[Tons])
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.
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]())
)