Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.
Register now!The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more
Hi Experts!
I have a table (after few steps to the original table) with below columns (sample):
| Currency | YearMonth | Sales |
| USD | 201912 | 1.1234 |
| CZK | 201912 | 25.408 |
| GBP | 201912 | 0.8508 |
| RON | 201912 | 4.783 |
| CNY | 201912 | 7.8205 |
| EUR | 201912 | 1 |
| USD | 202001 | 1.1052 |
| CZK | 202001 | 25.21 |
| GBP | 202001 | 0.84175 |
| RON | 202001 | 4.7789 |
| CNY | 202001 | 7.6664 |
| EUR | 202001 | 1 |
| USD | 202002 | 1.0977 |
| CZK | 202002 | 25.39 |
| GBP | 202002 | 0.85315 |
| RON | 202002 | 4.813 |
| CNY | 202002 | 7.6662 |
| EUR | 202002 | 1 |
| USD | 202003 | 1.0956 |
| CZK | 202003 | 27.312 |
| GBP | 202003 | 0.88643 |
| RON | 202003 | 4.8283 |
| CNY | 202003 | 7.7784 |
| EUR | 202003 | 1 |
I need a custom column with below condition:
From YearMonth column, pick the last months' Sale, starting from January of that particular year to that particular month, including last year's December value, and take the average of those Sales according to that Currency.
I added a custom column with a code like this:
let
currency = [Currency],
year = Number.FromText(Text.Start(Text.From([YearMonth]), 4)),
month = Number.FromText(Text.End(Text.From([YearMonth]), 2)),
months_to_include = List.Numbers(
if month < 7 then 12 - month + 1 else 1,
if month < 7 then 6 else month,
1
),
last_year_rows = Table.SelectRows(
#"Removed Other Columns",
each [Currency] = currency and
Number.FromText(Text.Start(Text.From([YearMonth]), 4)) = year - 1 and
Number.FromText(Text.End(Text.From([YearMonth]), 2)) > 6 - month and
Number.FromText(Text.End(Text.From([YearMonth]), 2)) <= 12
),
current_year_rows = Table.SelectRows(
#"Removed Other Columns",
each [CURRENCY_ID] = currency and
Number.FromText(Text.Start(Text.From([YearMonth]), 4)) = year and
Number.FromText(Text.End(Text.From([YearMonth]), 2)) <= month
),
all_rows = current_year_rows & last_year_rows,
filtered_rows = Table.SelectRows(
all_rows,
each [YearMonth] >= Number.ToText(year * 100 + months_to_include{0}, "0000") and [YearMonth] <= [YearMonth]
),
avg_Sales = List.Average(filtered_rows[Sales])
in
avg_Sales
But now, I although values are correct after July, from January to June values are NULL. Can someone please help me to fix this?
I understand the error should be in "months_to_include", but I do not know how to fix it. Thank you!
Solved! Go to Solution.
This type of calculation is much easier with DAX (and a Date column vs. date integer). Have you already tried that?
Pat
This type of calculation is much easier with DAX (and a Date column vs. date integer). Have you already tried that?
Pat
Yes, I added a calculated column like this but it gives wrong values.
Average=
VAR currentYearMonth = [YearMonth]
VAR currentMonth = RIGHT(currentYearMonth, 2)
VAR currentYear = LEFT(currentYearMonth, 4)
VAR lastYearMonth = IF(currentMonth = "01", FORMAT(VALUE(currentYear) - 1, "0000") & "12", FORMAT(VALUE(currentYear), "0000") & FORMAT(VALUE(currentMonth) - 1, "00"))
VAR numMonths = VALUE(currentMonth) + (12 * (VALUE(currentYear) - VALUE(LEFT(lastYearMonth, 4))))
RETURN
CALCULATE(
AVERAGE('table'[Sales]),
FILTER(
'table',
'table'[Currency] = EARLIER('table'[currency]) &&
'table'[YearMonth] >= lastYearMonth &&
'table'[YearMonth] <= currentYearMonth
)
)
Since there are other calculations those cannot be done in desktop, I need to do this in Power Query 😞
The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!
| User | Count |
|---|---|
| 19 | |
| 10 | |
| 9 | |
| 8 | |
| 7 |