Continuous subtracting of values from previous row in a different column - powerbi

I am working on a Power BI dashboard that is forecasting values up until 2030, depending on projects that are under implementation between 2022 to 2030.
I have one table [Actual] that contains actual values from 2021 and 2022 which I have at a monthly frequency.
I also have another table [Project Impacts] that contains the projects, with the impacts that they have on the actual values, and this is available from 2022 to 2030, at a quarterly frequency.
There are a few important measures relevant to this problem.
From the CO2 table, we have ‘Actual Values’.
From the Projects table, we have a measure named ‘Estimated saving from projects’, which calculates how much savings there are based on the projects each quarter. The table below shows the measures next to each other.
What I want to do is subtract the ‘Estimated savings from projects’ from the ‘Actual Values’, so that I am left with the ‘Forecast Values’. This is the results I get after doing this (image below).
As you can see from the image above, the ‘Forecast Values’ seems to be working properly, up until 2023/Q1. This is because there is no current data for 'Actual Values' after 2022, so the ‘Estimated saving from Projects’ is just subtracting from 0, hence the negative values in the far right column.
What I want to do is keep the calculation the same up until the end of 2022, but after 2022, the ‘Estimated saving from Projects’ should be subtracted from the last quarters value.
So as an example, the 2023/Q1 ‘Estimated saving from Projects’ value of 7.87 should be subtracted from the 53.51 value from the 2022/Q4 value in the ‘Forecast Value’ measure, giving a value of 45.64 instead of the value -7.57. Then the same process should repeat, so the 7.87 from 2023/Q2 should be subtracted from the 45.64 from the previous quarter and so on.
Does anyone know of a DAX formula, calculated column, or any solution to the problem above?
Many thanks

OK, so this isn't DAX, but I think the result works in your situation. I resolved it in Power Query. I recreated your table and added on to it:
+------+---------+--------------+------------------+
| Year | Quarter | ActualValues | EstimatedSavings |
+------+---------+--------------+------------------+
| 2021 | 2021Q1 | 83.88 | |
| 2021 | 2021Q2 | 72.84 | |
| 2021 | 2021Q3 | 72.25 | |
| 2021 | 2021Q4 | 68.13 | |
| 2022 | 2022Q1 | 77.53 | 0.17 |
| 2022 | 2022Q2 | 67.75 | 0.17 |
| 2022 | 2022Q3 | 63.08 | 0.17 |
| 2022 | 2022Q4 | 58.14 | 4.63 |
| 2023 | 2023Q1 | | 7.87 |
| 2023 | 2023Q2 | | 7.87 |
| 2023 | 2023Q3 | 104.33 | 8.94 |
| 2023 | 2023Q4 | | 5.11 |
| 2024 | 2024Q1 | | 10.08 |
| 2024 | 2024Q2 | | 10.08 |
| 2024 | 2024Q3 | | 9.04 |
| 2024 | 2024Q4 | | 8.42 |
+------+---------+--------------+------------------+
For illustration purposes, I then created a column I called "ForeCastValuesOLD". You will see below that instead of negative values, you get a "null" value, because the empty cells in "ActualValues" translate to a "null" value in Power Query (expected behavior for a column formatted as decimal number), so subtracting "EstimatedSavings" from this results in another "null" value. It's still wrong in terms of what you want, but it's a "different wrong".
I then selected the "ActualValues" column, and on the "Transform" tab clicked on the arrow next to "Fill" and then on "Down".
Note: "Fill | Down" will not work if you have zeros in that column, it only works with null values. See also here.
As a last step, I repeated the ForeCastValues calculated column, only with the new "ActualValues" column. The results are what you are looking for, I believe, and it doesn't matter how many blanks you have or where they are in that column.
This is the final table:
+------+---------+--------------+------------------+-------------------+----------------+
| Year | Quarter | ActualValues | EstimatedSavings | ForeCastValuesOLD | ForeCastValues |
+------+---------+--------------+------------------+-------------------+----------------+
| 2021 | 2021Q1 | 83.88 | | | |
| 2021 | 2021Q2 | 72.84 | | | |
| 2021 | 2021Q3 | 72.25 | | | |
| 2021 | 2021Q4 | 68.13 | | | |
| 2022 | 2022Q1 | 77.53 | 0.17 | 77.36 | 77.36 |
| 2022 | 2022Q2 | 67.75 | 0.17 | 67.58 | 67.58 |
| 2022 | 2022Q3 | 63.08 | 0.17 | 62.91 | 62.91 |
| 2022 | 2022Q4 | 58.14 | 4.63 | 53.51 | 53.51 |
| 2023 | 2023Q1 | 58.14 | 7.87 | | 50.27 |
| 2023 | 2023Q2 | 58.14 | 7.87 | | 50.27 |
| 2023 | 2023Q3 | 104.33 | 8.94 | 95.39 | 95.39 |
| 2023 | 2023Q4 | 104.33 | 5.11 | | 99.22 |
| 2024 | 2024Q1 | 104.33 | 10.08 | | 94.25 |
| 2024 | 2024Q2 | 104.33 | 10.08 | | 94.25 |
| 2024 | 2024Q3 | 104.33 | 9.04 | | 95.29 |
| 2024 | 2024Q4 | 104.33 | 8.42 | | 95.91 |
+------+---------+--------------+------------------+-------------------+----------------+
And here is the M code:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fc5LDsMgDATQu7CORvgDdo7RdcT9r1EMjdSWJCuP9MayjyNxZkrbGK8ILnDvM7XtF7kHY7heoUzkcoWxUR0kX8gTedw0QwnMIFsKEarByl0hNqsg+10h7pd+P6aiylmQWZDxQ7wBt8X4wSJQVkgEx65LQ+d2AdFpOk3Pq5Tj9X/kJ5SJO7Iu9rnoUE6tvQE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Year = _t, Quarter = _t, ActualValues = _t, EstimatedSavings = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Quarter", type text}, {"ActualValues", type number}, {"EstimatedSavings", type number}}),
#"Added Custom1" = Table.AddColumn(#"Changed Type", "ForeCastValuesOLD", each [ActualValues] - [EstimatedSavings]),
#"Changed Type2" = Table.TransformColumnTypes(#"Added Custom1",{{"ForeCastValuesOLD", type number}}),
#"Filled Down" = Table.FillDown(#"Changed Type2",{"ActualValues"}),
#"Added Custom" = Table.AddColumn(#"Filled Down", "ForeCastValues", each [ActualValues] - [EstimatedSavings]),
#"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"ForeCastValues", type number}})
in
#"Changed Type1"

Related

Opening Stock Value In Powerbi

I am working on Cost of goods sold using Powerbi.
https://www.mediafire.com/file/pmb7u1thsag1kq1/Cost+of+goods+sold.pbix/file
Above is my file which i uploaded on mediafire.
i am taking the average price by using average function year wise.
If you see in File.
| GName | Year | Opening Stock | InQty | OutQty | InItemValue | Average Value | Closing Stock | Closing Stock Value | Opening Stock Value | Cost of Goods Sold |
|-------------|------|---------------|-------|--------|-------------|---------------|---------------|---------------------|---------------------|--------------------|
| Bahria Town | 2016 | | 4454 | 3586 | 126610299.8 | 28426.20113 | 868 | 24673942.58 | 0 | 101936357.2 |
| Bahria Town | 2017 | 868 | 6379 | 6547 | 166903971.5 | 23030.76743 | 700 | 16121537.2 | 0 | 150782434.3 |
| Bahria Town | 2018 | 700 | 9129 | 8709 | 271932546.3 | 27666.3492 | 1120 | 30986311.11 | 0 | 240946235.2 |
| Bahria Town | 2019 | 1120 | 9333 | 9393 | 313226466.8 | 29965.22212 | 1060 | 31763135.45 | 0 | 281463331.4 |
| Bahria Town | 2020 | 1060 | 10192 | 10136 | 362950101.2 | 32256.49673 | 1116 | 35998250.35 | 0 | 326951850.8 |
| Bahria Town | 2021 | 987 | 8882 | 8468 | 404199067.4 | 40956.43605 | 1530 | 62663347.16 | 0 | 346819100.5 |
In Above as you can see i just took the Average Value
Average Value = ([Opening Stock Value]+[initemvaluee])/([inqtyy]+[Opening Stock])
and closing stock value
Closing Stock Value = [Average Value] * [Closing Stock]
When i calculate the closing stock value of previous year its give me error.
circular dependency was detected: Measure: 'mak_stockInHandValue'[Average Value], Measure: 'mak_stockInHandValue'[Opening Stock Value], Measure: 'mak_stockInHandValue'[Average Value].
Any Suggestion to see the closing stock value in the field of opening stock value ?????
I am working on this from more than 2 weeks.
Please help me out
Thanks in advance

Power Bi, Dax - calculations, filters, balance

Could you please help me to solve the problem as I am totally new to DAX and English is not my first language so I am struggling to even find the correct question.
Here's the problem.
I have two tables:
start_balance
+------+---------------+
| Type | Start balance |
+------+---------------+
| A | 0 |
| B | 10 |
+------+---------------+
in_out
+------+-------+------+----+-----+
| Year | Month | Type | In | Out |
+------+-------+------+----+-----+
| 2020 | 1 | A | 20 | 20 |
| 2020 | 1 | A | 0 | 10 |
| 2020 | 2 | B | 20 | 0 |
| 2020 | 2 | B | 20 | 10 |
+------+-------+------+----+-----+
I'd like to get the result as follows:
Unfiltered:
+------+-------+------+---------+----+-----+------+
| Year | Month | Type | Balance | In | Out | Left |
+------+-------+------+---------+----+-----+------+
| 2020 | 1 | A | 0 | 20 | 20 | 0 |
| 2020 | 1 | B | 10 | 20 | 10 | 20 |
| 2020 | 2 | A | 0 | 20 | 10 | 10 |
| 2020 | 2 | B | 20 | 20 | 10 | 30 |
+------+-------+------+---------+----+-----+------+
Filtered (for example year/month 2020/2):
+------+-------+------+---------+----+-----+------+
| Year | Month | Type | Balance | In | Out | Left |
+------+-------+------+---------+----+-----+------+
| 2020 | 2 | A | 0 | 20 | 10 | 10 |
| 2020 | 2 | B | 20 | 20 | 10 | 30 |
+------+-------+------+---------+----+-----+------+
So while selecting a slicer for the year/month it should calculate balance before selected year/month and then show selected year/month values.
Edit: corrected start_balance table.
Is the sample data correct?
A -> the starting balance is 10, but in your unfiltered table example, it is 0.
Do you have any relationship between these tables?
Does opening balance always apply to the current year? What if 2021 appears in the in_out table? How do you know when the start balance started?
example without starting balance
If you want to show value breaking given filter you should use statement ALL or REMOVEFILTERS function (in Analysis Services 2019 and in Power BI since October 2019).
calculate(sum([in]) - sum([out]), all('in_out'[Year],'in_out'[Month]))
More helpful information:
https://www.sqlbi.com/articles/managing-all-functions-in-dax-all-allselected-allnoblankrow-allexcept/

How do i add additional rows in M QUERY

I want to add more rows using the Query editor (Power query/ M Query) in only the Start Date and End Date column:
+----------+------------------+--------------+-----------+-------------+------------+
| Employee | Booking Type | Jobs | WorkLoad% | Start Date | End date |
+----------+------------------+--------------+-----------+-------------+------------+
| John | Chargeable | CNS | 20 | 04/02/2020 | 31/03/2020 |
| John | Chargeable | CNS | 20 | 04/03/2020 | 27/04/2020 |
| Bernard | Vacation/Holiday | SN | 100 | 30/04/2020 | 11/05/2020 |
| Bernard | Vacation/Holiday | Annual leave | 100 | 23/01/2020 | 24/02/2020 |
| Bernard | Chargeable | Tech PLC | 50 | 29/02/2020 | 30/03/2020 |
+----------+------------------+--------------+-----------+-------------+------------+
I want to find the MIN(Start Date) and MAX(End Date) and then append the range of start to end dates to this table only in the Start Date and End Date column in the Query Editor (Power Query/ M Query). Preferrable if I can create another table2 duplicating the original table and append these rows.
For example:
+----------+------------------+--------------+-----------+-------------+------------+
| Employee | Booking Type | Jobs | WorkLoad% | Start Date | End date |
+----------+------------------+--------------+-----------+-------------+------------+
| John | Chargeable | CNS | 20 | 04/02/2020 | 31/03/2020 |
| John | Chargeable | CNS | 20 | 04/03/2020 | 27/04/2020 |
| Bernard | Vacation/Holiday | SN | 100 | 30/04/2020 | 11/05/2020 |
| Bernard | Vacation/Holiday | Annual leave | 100 | 23/01/2020 | 24/02/2020 |
| Bernard | Chargeable | Tech PLC | 50 | 29/02/2020 | 30/03/2020 |
| | | | | 23/01/2020 | 23/01/2020 |
| | | | | 24/01/2020 | 24/01/2020 |
| | | | | 25/01/2020 | 25/01/2020 |
| | | | | 26/01/2020 | 26/01/2020 |
| | | | | 27/01/2020 | 27/01/2020 |
| | | | | 28/01/2020 | 28/01/2020 |
| | | | | 29/01/2020 | 29/01/2020 |
| | | | | 30/01/2020 | 30/01/2020 |
| | | | | 31/01/2020 | 31/01/2020 |
| | | | | ... | ... |
| | | | | 11/05/2020 | 11/05/2020 |
+----------+------------------+--------------+-----------+-------------+------------+
The List.Dates function is pretty useful here.
Generate the dates in your range, duplicate that to two columns and then append.
let
StartDate = List.Min(StartTable[Start Date]),
EndDate = List.Max(StartTable[End Date]),
DateList = List.Dates(StartDate, Duration.Days(EndDate - StartDate), #duration(1,0,0,0)),
DateCols = Table.FromColumns({DateList, DateList}, {"Start Date", "End Date"}),
AppendDates = Table.Combine({StartTable, DateCols})
in
AppendDates

Power Bi, compare a text column by month

I need a little help
+---------------------------------+-----------------+---------------+------------+
| Name | Opening Balance | Close Balance | Date |
+---------------------------------+-----------------+---------------+------------+
| LEAL MANZANO ABUNDIO | 394,732.87 | 406,866.31 | 31/08/2018 |
| LOPEZ GRANADOS CLAUDIA CAT | 382,567.83 | 382,567.83 | 31/08/2018 |
| ABARCA RODRIGUEZ ERNESTO | 394,142.32 | 394,142.32 | 31/08/2018 |
| OSOLLO JUAREZ PALOMA | 396,030.58 | 396,030.58 | 31/08/2018 |
| MACHUCA HERNANDEZ GUILLERM | 410,809.87 | 422,943.31 | 31/08/2018 |
| LEAL MANZANO ABUNDIO | 406,866.31 | 409,466.22 | 30/09/2018 |
| LOPEZ GRANADOS CLAUDIA CATALINA | 382,567.83 | 382,567.83 | 30/09/2018 |
| ABARCA RODRIGUEZ ERNESTO | 394,142.32 | 394,142.32 | 30/09/2018 |
| OSOLLO JUAREZ PALOMA | 396,030.58 | 396,030.58 | 30/09/2018 |
| MACHUCA HERNANDEZ GUILLERMO | 422,943.31 | 0 | 30/09/2018 |
| MACIAS SANCHEZ JOSE | 425,457.57 | 425,457.57 | 30/09/2018 |
| PARDINEZ BUCIO EDUARDO | 434,591.25 | 434,591.25 | 30/09/2018 |
| LEAL MANZANO ABUNDIO | 409,466.22 | 0 | 31/10/2018 |
| LOPEZ GRANADOS CLAUDIA CATALINA | 382,567.83 | 382,567.83 | 31/10/2018 |
| ABARCA RODRIGUEZ ERNESTO | 394,142.32 | 394,142.32 | 31/10/2018 |
| OSOLLO JUAREZ PALOMA | 396,030.58 | 396,030.58 | 31/10/2018 |
| MACHUCA HERNANDEZ GUILLERMO | 0 | 0 | 31/10/2018 |
+---------------------------------+-----------------+---------------+------------+
So i have this table with clients Names and dates, i need to compare how it changed month by month, to know how many ins and outs i had.
Thank you.
Samuel, I loaded your table into PowerBI and created the following visuals
This is accomplished by adding a new calculated column "Client Change." This basically adds a column that just puts in a field that identifies a new balance when opening is 0 and closing is > 0. Conversely, it sets the field to 'close balance' when thee opening balance > 0 and the closing = 0. I plunk it into a matrix and use the month grain from the native date hierarchy against the names. Some data fixing on the names is needed for truncated strings in your data set.
ClientChange =
if(AND([ Opening Balance ] = 0, [ Close Balance ] > 0 )
, "NewBalance"
, if(AND ([ Opening Balance ] > 0, [ Close Balance ] = 0)
, "Balance Closed"
, " -- "
)
)
I also added a measure 'client count' that counts all the rows where closing balance isn't 0.
ClientCount = COUNTX(FILTER(testData,[ Close Balance ] <> 0), testData[Name])
Hope it helps. Please note that there is an oddity with 'MACIAS SANCHEZ JOSE' -- he has a record in September but not in August or October. His neither his open nor close balance is 0. It doesn't quite make sense.

PowerQuery PowerBI merge 2 tables based on condition between StartDate and EndDate

I try to join 2 tables using Power Query/PowerBI: Absence and dimDate to create a result table below:
Absence table
+------------+--------------+--------------+-----------+-----------+
| EmployeeId | EmployeeName | AbsenceType | StartDate | EndDate |
+------------+--------------+--------------+-----------+-----------+
| 1 | A | Annual Leave | 2/01/2017 | 5/01/2017 |
| 2 | B | Sick Leave | 4/01/2017 | 6/01/2017 |
+------------+--------------+--------------+-----------+-----------+
dimDate table
+------------+
| FullDate |
+------------+
| 1/01/2017 |
| 2/01/2017 |
| 3/01/2017 |
| 4/01/2017 |
| 5/01/2017 |
| 6/01/2017 |
| 7/01/2017 |
| 8/01/2017 |
| 9/01/2017 |
| 10/01/2017 |
+------------+
Result
+------------+--------------+--------------+-----------+
| EmployeeId | EmployeeName | AbsenceType | Date |
+------------+--------------+--------------+-----------+
| 1 | A | Annual Leave | 2/01/2017 |
| 1 | A | Annual Leave | 3/01/2017 |
| 1 | A | Annual Leave | 4/01/2017 |
| 1 | A | Annual Leave | 5/01/2017 |
| 2 | B | Sick Leave | 4/01/2017 |
| 2 | B | Sick Leave | 5/01/2017 |
| 2 | B | Sick Leave | 6/01/2017 |
+------------+--------------+--------------+-----------+
I usually use SQL to create this result, however I don't know how to efficiently do it in PowerQuery.
SELECT A.EmployeeId
,A.EmployeeName
,A.AbsenceType
,D.FullDate
FROM Absence AS A
INNER JOIN dimDate AS D ON (
D.FullDate >= A.StartDate
AND D.FullDate <= A.EndDate
)
Note: I have tried the Full Join between 2 tables Absence and dimDate, then filter true value if dimDate.FullDate >= StartDate and dimDate.FullDate <= EndDate. However this approach seems to be ineffective with large table, and it creates redundant records before filtering so it's quite slow.
Please give me some advice.
No need to merge. You can create a column with embedded lists of all dates between StartDate and EndDate. And then expand that column.
let
Source = Table1,
#"Added Custom" = Table.AddColumn(Source, "Date", each List.Dates([StartDate],1+Duration.Days([EndDate]-[StartDate]),#duration(1,0,0,0))),
#"Expanded Date" = Table.ExpandListColumn(#"Added Custom", "Date"),
#"Changed Type" = Table.TransformColumnTypes(#"Expanded Date",{{"Date", type date}}),
#"Removed Columns" = Table.RemoveColumns(#"Changed Type",{"StartDate", "EndDate"})
in
#"Removed Columns"