Recreate pivot table (or table visualisation) as new table using DAX - excel

I am a beginner in PowerBI looking to better understand the application and look for ways to upgrade the Excel dashboards that I made for a company of a friend. Therefore, I am trying to recreate the Excel dashboard in PowerBI. I am doing quite well so far with the other visualisation and the data model, but I am struggling to recreate a visualisation for which I had to create a Pivot table in Excel.
Although I can recreate the pivot table using the table visualisation, I would like to create an actual table using DAX such that I can use it for other visualisations as well.
I have a fact table called fJobs that has a structure:
Date
OrderNumber
License plate
StartTime
EndTime
Duration
01-01-2021
1
plate 1
08.00
12.00
03.00
01-01-2021
2
plate 1
11.00
12.00
01.00
01-01-2021
3
plate 2
13.00
15.00
02.00
02-01-2021
4
plate 2
09.00
15.00
06.00
03-01-2021
5
plate 1
09.00
16.00
07.00
03-01-2021
6
plate 2
08.00
10.00
02.00
03-01-2021
7
plate 2
10.00
15.30
05.30
03-01-2021
8
plate 2
15.30
16.00
00.30
As you can observe, for a particular day a license plate can be scheduled for multiple orders. Now I would like to create a table that calculates the utilisation rate is (having summed the duration of multiple orders for a truck on a specific day) for each day for each license plate. It would look like:
Utilisation rate per day per truck
license plate
01-01-2021
02-01-2021
03-01-2021
plate 1
0.50
0.0
0.875
plate 2
0.25
0.75
1.0
Where the license plate column is a column of a dimension table (dTrucks), the dates are a column of another dimension table (dCalender) but these are now transposed and filtered on weekdays. The values are a sum of the duration of jobs per day for a particular license plate (truck), divided by 8 (working day).
I can recreate the latter table in PowerBI using the table visualisation with the following settings:
values: dTrucks[License plate]
Columns: dCalender[Date]
Values: fJobs[Duration] divided by 8 (quick measure)
How would I go about creating such a table using DAX (new table) in PowerBI?Thanks in advance!
John

Related

Yield to maturity / IRR in Power Query M

Do you have any ideas about how to calculate YTM / IRR in Microsoft Power Query?
For instance, I have a variable called Table and it contains data
Date Payment
2020-01-01 -1000
2020-02-01 300
2020-03-01 800
and I would like to call some scalar function, which calculates YTM.
Seems like now the only way is to solve this equation yourself. I've found code in this blog post. I have a laptop with Win 10, Excel 2016, i3-6100 2.3GHz, 4 ram. It takes 0.10 sec to calculate for deal with 10 payments.

Calculated field totals not correct in PowerPivot

I have a basic data set with a ton of slicers that roughly looks like this:
Hours SpreadPerHr Spread
5.00 5.00 25.00
10.00 2.00 20.00
8.00 10.00 80.00
Where Spread is a calculated value where Spread = Hours*SpreadPerHour. The problem is, the totals for these columns follow this formula too, so it looks like this:
Hours SpreadPerHr Spread
5.00 5.00 25.00
10.00 2.00 20.00
8.00 10.00 80.00
Total: 23.00 17.00 391.00
And while the hours total up just fine, SpreadPerHour is dynamic and so Spread is as well. It is incorrect to say Total Spread = Total Hours * Total SpreadPerHour. Totals should be:
Total: 23.00 17.00 125.00
Is there a way I can make excel leave totals for Hours as-is, but sum the column for Spread instead of multiplying totals?
Here is what I think you have in your Power Pivot Model:
You have a calculated measure for Spread, which I have labeled SpreadCalc1. The problem with this is that it does the aggregation before it does the multiplication. You need this operation to be done on a row-by-row basis and then aggregated. So instead of a calculated measure, you need to create a calculated column and then sum that column.
The column I have labeled as SpreadCalc has the formula =[Hours] * [SpreadPerHr].
The calculated measure I called Spread is just Sum([SpreadCalc]). You can see there that the total is 125 as desired instead of 391.
I know this might be a bit redundant now, but I would suggest a slightly different approach.
Adding calculated columns in "small" tables is fine, but it can cause serious performance issues with large databases.
So to solve your problem, I believe the "correct" way is to use SUMX function.
It calculates the expression specifically for each row, which is exactly what you need. And it is smart as far as performance goes (no need to add calculated columns or perform any source-data manipulations).
If you use this formula (correct the name of the table / measures), you should get the desired results:
SUMX(YourTable, [Sum Hour] * [Sum SpreadPerHr])

Pulling Data in to Excel from SQL Server 2012 and formatting it

I will try and explain as best I can what i am trying to do but I am no Excel genius :-)
I have 1 Excel Sheet which we will call Template this is going to be pre-populated with data and then used offline, I am going to import a Data Set in to a tab called RawData from SQL Server, data set will be something similar to this
Description Rate Hours SellValue Item
APU 2.50 3 7.50 1
APU 2.50 4 10.00 2
APU 2.50 5 12.50 3
INS 2.50 3 7.50 1
INS 2.50 4 10.00 2
INS 2.50 5 12.50 3
There could be more or less records but no more than 7 distinct descriptions.
There is now another Tab called Report
At Position A1 will be the Title APU then underneath that I want the records with the Description APU to appear and this block will shrink and expand depending on the number of records. Then wherever the last record appears will be the next heading INS and the records associated with that description. There is one final tab called Rate and at position A1 will be a rate value, when this is changed, it will take the new rate value and re-calculate the SaleValue in RawData thus amending the figures in the Report. I am sure there are better ways to explain this but I hope someone has got the gist before I loose what hair I have left.
Thanks in advance
The easiest and powerful approach to such problems would be to use Pivot-Tables in excel. You can customize your data view better.

Excel Pivot Table - Averaging by Category

I'm trying to create an average by category in a pivot table. This is the first time I've created a pivot table so sorry if the answer is staring me in the face. My raw data looks like:
Date, Transaction type, Description, Paid out, Paid in, Balance, Category
Mar-13, Visa, SHOP, £4.44, , £X, Gifts
Mar-13, Visa, SHOP, £5.00, , £Y, Children
Mar-13, Visa, SHOP, £6.00, , £Z, Gifts
Mar-13, Visa, CLOTHES SHOP YORK, £8.00, , £A, Clothing
Mar-13, Visa, FOOD SHOP, £11.96, , £B, Food
My pivot table shows the information rolled up by Month and grouped by a category:
Row Labels Sum of Paid out Sum of Paid in Sum of Difference
2013
Jan £Jan £Jan £C
Food 1 2 -
Car 2 3 -
Cash 6 6
Feb £Feb £Feb £D
Food 1 2
Car 8 0
Cash 2 3
The categories/data is made up in this case, but the desired outcome I'm after is to get an annual average, informing me how much comes in/out on average across the year per category... looking something like:
Row Labels Sum of Paid out Sum of Paid in Sum of Difference
2013
Avg £AvgIn £AvgOut £AvgDiff
Food 1 2 -
Car 5 1.5 -
Cash 4 4.5
Jan £Jan £Jan £C
Food 1 2 -
Car 2 3 -
Cash 6 6
Feb £Feb £Feb £D
Food 1 2
Car 8 0
Cash 2 3
Is this possible to achieve using a pivot table, as I can't seem to find a way to this at the moment using Excel 2010.
Since it appears that your raw data is already grouped by month, you're able to do this pretty easily. You need to re-arrange your data, however -- the months and the categories need to be on different axes. For example, Category as ColumnLabels, with Values then Month as Row Labels. Then right-click one of the normal values for Paid Out, choose "Summarize Value As...", you'll see SUM is currently checked, just change to AVERAGE. Repeat for one of the Paid In values. The labels should change to help let you know if it worked.
Note that this will NOT work effectively if your source data comes in daily, for example. With only one entry per month, the SUM and the AVERAGE of the single entry are identical. This would not be the case if your raw data was daily (and you still grouped by month) -- you'd be switching from a monthly total to a daily average.
I am currently using LibreOffice, which is just the free version of Excel on a mac. It is basically the same thing, but anyways, do you see the top bar where it says =AVERAGE(B8,C8,D8)? Well, that is how you can incorporate the functions within the table. This is actually a pivot table I had to do for my Java class. All you have to do is click on the cell you want to edit, type =, the function name, (in your case, AVERAGE should be fine) and then any other kinds of functions you use. Hope this helps you out.

Powerpivot: Joining historicals for database and forecast from a calculated linked table in a single pivottable

I noticed a few people have tried to do similar things that I am asking below. I may have a suggestion in my answer as to how I fix it so i'll ask the question first.
I have historical data that I source from a datawarehouse connection and load into Powerpivot:
Date Store Sales NumTransactions Tag
2010-01-01 A 20000 50 Historical
2010-01-01 B 15000 10 Historical
2010-01-01 C 5000 3 Historical
2010-01-01 D 300000 500 Historical
That connection to Powerpivot is a direct connection to the database server.
And from this data, i calculate a forecast, within excel that spits out a table that is then brought into PowerPivot using a linked table.
Date Store Sales NumTransactions Tag
2010-01-02 A 20010 51 Forecast
2010-01-02 B 16000 12 Forecast
2010-01-02 C 6000 5 Forecast
2010-01-02 D 400000 650 Forecast
From this I want to be able to drive a number of pivot tables and charts which show both sets of data together:
Date Store Sales NumTransactions Tag
2010-01-01 A 20000 50 Historical
2010-01-01 B 15000 10 Historical
2010-01-01 C 5000 3 Historical
2010-01-01 D 300000 500 Historical
2010-01-02 A 20010 51 Forecast
2010-01-02 B 16000 12 Forecast
2010-01-02 C 6000 5 Forecast
2010-01-02 D 400000 650 Forecast
How do i make this happen? I cant seem to make a relationship work as there is multiple dates and multiple store records.
I have tried to join the two together at within Powerpivot in Table Properties -> Query Editor:
SELECT [daily].* FROM [daily]
UNION ALL
SELECT [fcst].* from [fcst]
(Where [daily] is the historical data from the datawarehouse and [fcst] is the linked table within the excel workbook that has been linked into powerpivot)
But the problem is that Powerpivot is looking for [fcst] from the Connection that goes back to the datawarehouse where my historicals are coming from.
I can make a work around of driving two pivot tables out of the two data sources and then link them together into one table in excel by directly referencing the cells of the pivot tables (and then drive charts off that). But its messy, and Im sure this can be done. I just cant figure out how.
Does the answer lie in a Set defined by MDX?
Any ideas would be greatly appreciated.
Thanks!
When PowerPivot creates a relationship, it needs one table to have a column with distinct values that you use as a lookup column for another table. I would suggest that you create a date table. You can get one from https://datamarket.azure.com/dataset/1542c52d-d466-4094-a801-9ef8fd7c6485 if you don't have an easy way to create one. Then you can create a relationship from daily to date and from fcst to date. I would also add a calculated column to daily with just a string "Historical" and add a calculated column to fcst for a string "Forecast". Add another linked table with two rows in it: Historical, Forecast. Then create a relationship from daily to this linked table. If you create a flattened PivotTable with all this in place - you can get close to what you're looking for, but you're probably going to have one column for sales for historical and another column for sales for forecast. You can create a measure in one of the tables (doesn't matter which one) to use conditional logic to use the applicable value based on the current tag and just use that in your PivotTable instead. Do the same for the number of transactions.
You can also add a table of month end dates that is not linked to any other tables and use that date as a slicer with Dates equal to or less than the Slicer, select Actuals and Dates greater than the Slicer, use the Forecast. In doing this you avoid picking up a partial month of actuals for an incomplete month. Then add a measure such as...
calculate(sum(historical[sales], filter(historical[date]<=max(monthend[Dates]))) + calculate(sum(forecast[sales], filter(forecast[sales]> max(monthend[Dates])))

Resources