Running Total with Additional Field Filter in Power BI - filter

I have created a running total measure in PBI using Dax, however, when the total does not filter by column filter when it is in the table. The running total should sum the balance but then break out into the individual maturity buckets within the table.
Here is the Measure and the resulting table
I have tried adding extra filters using the FILTER/ALL command to break out the maturity buckets and gotten either the same result or errors. Not sure what else I can do?
Here is a fake sample of data. in the comments i have include the language of the measure.
|Date |Tenor|Balance |
|-------------|-----|-----------------|
|December 2022|18m |0.196072326627487|
|December 2022|2y |0.149643186475954|
|December 2022|3y |0.180522608363889|
|December 2022|4y |0.780540306321475|
|December 2022|5y |0.156029893270158|
|January 2022|18m |0.512496934496972|
|January 2023|2y |0.068123785829084|
|January 2023|3y |0.349971677118287|

Here is my solution! I am sorry if I kept you waiting too much!
First, I need to say that your design is not so efficient! You need a full date table with full date values(not only month &year parts)
Here is your calendar table:
Here is the DAX Code you need to write to obtain correct value:
Total_Correct =
CALCULATE (
SUM('Callable'[Balance]),
FILTER ( ALL ( 'Callable'[Date] ), 'Callable'[Date] <= MAX('Callable'[Date])),
ALL('Calendar')
)

Related

Calculate Total Amount for a specific period

I want to calculate total value between two dates in DAX. I have a formula SUM(C5:C16) in excel sheet, which C5 is the sales amount for a specific date (last year + 1month), and C16 is the sales amount for current row date.
I tried this formula in DAX, but it did not return sum value:
var Rolling = CALCULATE(sum('proces'[HOURS]),DATESINPERIOD('Date'[DateField],ENDOFMONTH('proces'[date_start]),-12,MONTH))
Also, I tried this one, but it is not working:
=SumX (
var prev=DATEADD(DATEADD('proces'[date_start] ,-1,YEAR),+1,MONTH)
return
Filter ( 'proces',
'proces'[date_end] <= Earlier ( 'proces'[date_end] ) &&
'proces'[date_start]>=prev,
'proces'[HOURS])
Also, I tried this one but it returns nothing
=CALCULATE(
SUMX('proces','proces'[HOURS]),
DATESBETWEEN(
'Date'[DateField],
STARTOFMONTH(DATEADD(LASTDATE('Date'[DateField]),-1,MONTH)),
ENDOFMONTH(DATEADD('Date'[DateField],-1,MONTH))
)
)
You seem to be confused about variables in DAX and your formulas are not even valid DAX expressions. Learn about variables in the official documentation:
Use variables to improve your DAX formulas
If you need further help with calculating the your total amount, add sample data to your question.

Wrong sorting while using Query function

I've been trying to do a report about the quantity of breakdonws of products in our company. The problem is that the QUERY function is operating as normal, but the sorting order is well - a bit strange.
The data I'm trying to sort are as follows (quantities are blacked out since I cannot share those informations):
Raw data
First column - name of the product, second, it's EAN code, third, breakdown rate for last year, last column - average breakdown rate. "b/d" means "brak danych" or no data.
What I want to achieve is to get the end table with values sorted by average breakdown rate.
My query is as follows:
=query(Robocze!A2:D;"select A where A is not null and NOT D contains 'b/d' order by D desc")
Final result
As You can see, we have descending order, but there are strange artifacts - like the 33.33% after 4,00% and before 3,92%.
Why is that!?
try:
=INDEX(LAMBDA(x; SORT(x; INDEX(x;; 4)*1; 0))
(QUERY(Robocze!A2:D; "where A is not null and NOT D contains 'b/d'"; 0));; 4)

Split up date range into chunks to join back into one table KDB+/Q

I have a table that is being joined like so
result: select from table where date within (sd;ed)
where sd and ed span multiple months (like sd:2021.07.01 ed:2021.09.30). The table that I'm querying from has a break if you take more than a month, so to get the result I need, I have to do something like the following:
result: uj(uj(select from table where date within (2021.07.01;2021.07.30);select from table where date within (2021.08.01;2021.08.31));select from table where date within (2021.09.01;2021.09.30))
How can I make this dynamic for any sd and ed? That is, how can I break up time range into first days of months, last days of months, and join them all into one table cleanly? My initial idea was to divide the days in the range x amount of time, to be input by a user, then add the number of days that results to the sd to get frames, but that got messy.
Something like this should chunk it for you:
raze{select from t where date within x}each(first;last)#\:/:d group"m"$d:sd+til 1+ed-sd
Do not use where date.month=x as you had suggested - at least not for historical queries
One option for converting your start and end dates into an iterable list of dates might be:
f:{0N 2#(x,raze -1 0+/:`date$mx+1+til(`month$y)-mx:`month$x),y}
Where x is start date and y is end date.
f[2021.07.14;2022.02.09]
2021.07.14 2021.07.31
2021.08.01 2021.08.31
2021.09.01 2021.09.30
2021.10.01 2021.10.31
2021.11.01 2021.11.30
2021.12.01 2021.12.31
2022.01.01 2022.01.31
2022.02.01 2022.02.09
Then you could run:
{select from t where date within x} each f[sd;ed]
And join the results using raze or (uj/)

DAX formula to subtract columns

I am trying to calculate month over month difference but it makes data negative.
I created a measure, but it makes source data negative.
CALCULATE (
COUNTA ( SOURCE_DATA[COLUMN] ),
FILTER ( SOURCE_DATA, SOURCE_DATA[YYYYMM] = "201906" )
)
- (
CALCULATE (
COUNTA ( SOURCE_DATA[COLUMN] ),
FILTER ( SOURCE_DATA, SOURCE_DATA[YYYYMM] = "201905" )
)
)
The outcome is correct, but it changes data in previous month to negative.
This is due to the filter context and the way you've written the measure.
Look at the visual table. For the field corresponding to Column = 201905 and row = GA you get -16 813. This is because the context of the visual table tells CALCULATE to COUNTA(SOURCE_DATA[Column]) only when MtM = GA and Columns = 201905. However, adding the FILTER you also tell CALCULATE to keep these criteria AND also make sure that SOURCE_DATA[Column] = 201906 in the first calculate and 201905 in the second one.
This results in CALCULATE looking for rows where Column is both 201905 and 201906 at the same time. Or in other words you generate a venn diagram with no overlapping fields. Therefore the first calculate evaluates to 0 and the second to 16 813, so that the measure is actually evaluating 0-16813 = -16 813.
Since you didn't post any description of your data model I can inly guess what it looks like. However, since you're filtering on the SOURCE_DATA table I guess you don't use a Calendar table. This you should do! Have a calendar with a 1:* (1-to-many) relationship with the SOURCE_DATA and do filtering on the calendar. In addition you can have dynamically calculated day/week/month/year offsets so that you can create measures which don't have to be updated when there's a new month.
I think this video can be helpful: sqlbi videolecture
Also, have a look at this article: sqlbi filter in calculate

Power Query (M language) 50 day moving Average

I have a list of products and would like to get a 50 day simple moving average of its volume using Power Query (M).
The table is sorted by product name and date. I add a custom column and applied the code below.
if [date] >= #date(2018,1,29)
then List.Average(List.Range(Source[Volume],[Volume]-1,-50))
else ""
Since it is already sorted by date and name, an if statement was applied with a date as criteria/filter. However, an error occurs that says
'Volume' column not found in the table.
I expect to have an added column in the power query with volume 50 day moving average per product. the calculation to be done if date is greater than or equal Jan 29, 2018.
We don't know what your columns are, but assuming you have [product], [date] and [volume] in Source, this would average the last 50 days of [volume] for the identical [product] based on each [date], and place in a new column
AvgAmountAdded = Table.AddColumn(Source, "AverageAmount", (i) => List.Average(Table.SelectRows(Source, each ([product] = i[product] and [date]<=i[date] and [date]>=Date.AddDays(i[date],-50)))[volume]), type number)
Finally! found a solution.
First, apply Index by product see this post for further details
Then index again without criteria (index all rows)
Then, apply below code
= Table.AddColumn(#"Previous Step", "Volume SMA(50)", each if [Index_byProduct] >= 50 then List.Average(List.Range(#"Previous Step"[Volume], ([Index_All]-50),50)) else 0),
For large dataset, Table.Buffer function is recommended after index-expand step to improve PQ calculation speed

Resources