March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early bird discount ends December 31.
Register NowBe one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now
Hello!
I am looking to transform some invoice level data (where each row = 1 invoice) into a table showing spend per product per customer over time.
I'm confident there is some way to do this using a combination of unpivot & offset in power query, but as the # unpivoted rows required in each instance will vary (the contract duration and billing frequency varies per invoice) it is proving tricky.
To illustrate, here is a simplified version of the data I currently have:
And below is the format I would like to transform it into:
Ultimately, what I would like to be able to do is calculate revenue generated per customer and per product (monthly) over time.
If anyone knows how to get to the intended output, or could suggest a better way to do the analysis, I would very much appreciate the help!
Thanks in advance 🙂
Thanks for the replies from danextian and DataNinja777.
Hi @mia23719 ,
From your description I created simple tables:
1. Create the same index for the two tables above, I'm creating 1 here:
2. Merge two tables as a new table.
3. In the merged table, expand the column1 field.
4. Create a custom column.
= if [Billing] = "Monthly" then
if [Date.Column1] >= [Simplified Start Date] and [Date.Column1] <= Date.AddMonths([Simplified Start Date], Number.FromText(Text.Start([Contract Duration], 2))) then
[#"Billing Amount (£)"]
else
0
else if [Billing] = "Annually" then
if [Date.Column1] = [Simplified Start Date] or [Date.Column1] = Date.AddMonths([Simplified Start Date], Number.FromText(Text.Start([Contract Duration], 2))) then
[#"Billing Amount (£)"]
else
0
else
0
5. Perform a pivot operation.
6. You can remove the unneeded columns.
Result:
Best Regards,
Zhu
Community Support Team
If there is any post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Hi @mia23719 ,
For analyzing contracts with variable durations and start and end dates, I recommend using DAX instead of Power Query (M). There are multiple ways to achieve the desired output using DAX, and one of the approaches is outlined below:
2. Write a dax formula like below:
Total billing amount =
VAR BillingAmountPerRow =
ADDCOLUMNS(
VALUES('Calendar'[yyyy-mm]),
"BillingAmount",
CALCULATE(
SUMX(
'Contract Revenue',
IF(
'Contract Revenue'[Start Date] <= MAX('Calendar'[Date]) &&
'Contract Revenue'[End Date] >= MIN('Calendar'[Date]),
'Contract Revenue'[Billing amount],
BLANK()
)
)
)
)
RETURN
SUMX(BillingAmountPerRow, [BillingAmount])
3. Visualize in a matrix visual:
You can rearrange the rows and columns to achieve your desired output. I have attached an example pbix file for your reference.
Best regards,
Hi @mia23719
Why would you want to transform your data wihch is in a better shape into that format? You'll end up creating a measure for each month and you won't be able to slice and dice. You will need to generate several row of dates for those with monthly billing and then just two for annually which can be fairly easily done with Power Query and then just use a matrix viz to get the desired look.
That said, we would need a workable sample data and not an image. A link to an Excel file in the cloud will do.
Proud to be a Super User!
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!
Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.
Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.
User | Count |
---|---|
132 | |
90 | |
88 | |
64 | |
58 |
User | Count |
---|---|
203 | |
141 | |
107 | |
73 | |
70 |