Filter only to show durtion - powerbi

Time
Value
10/3/2022 18:21:40
correct
10/3/2022 18:22:50
incorrect
10/3/2022 18:28:00
correct
10/3/2022 18:34:00
incorrect
From the above table, I want only filter out and show on the table if the time difference between "correct" and "incorrect" is > 5 minutes

This assumes there is always only two alternating rows of correct and incorrect, and returns a column showing the duration in minutes between them. You can then filter that, since you were vague on how and what to filter
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Time", type datetime}, {"Value", type text}}),
#"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
#"Added Custom1" = Table.AddColumn(#"Added Index", "MinuteDuration", each if [Value] = "incorrect" then null else Duration.TotalMinutes(#"Added Index"{[Index]+1}[Time]-[Time]),type number),
#"Filled Down" = Table.FillDown(#"Added Custom1",{"MinuteDuration"})
in #"Filled Down"

Here's one way to solve your problem.
Make sure your date data is sorted chronologically ascending. (this can be done in Power Query.
Create an Index Column in Power Query
Close Power Query and add the following calculated column to your table
IfNextRowOverFiveMins =
var currentRow = 'Table (2)'[index] --this should be the index on your rable
var nextTime =LOOKUPVALUE('Table (2)'[time],'Table (2)'[index],currentRow+1) --this function looks up the time for the next entry
var timeDiff = DATEDIFF('Table (2)'[time],nextTime,MINUTE) --the datediff function finds the difference in days/hours/minutes between two date values
return IF(timeDiff>=5,TRUE(),FALSE())

Related

How to calculate time difference in Power BI

I have a table with a column having timestamp as it's value. I need to add a new column & calculate the time difference between 2 rows.
How to do this?
I'm new to this & want to know how to do it please
Bring your data into powerquery, like using data... from table/range
Add column, index column
Add column, custom column, formula
= try [Timestamp] - #"Added Index"{[Index]-1}[Timestamp] otherwise null
to be fancy manually edit code to end with null, type duration)
#"Added Index" = Table.AddIndexColumn(#"PriorStepNameHere", "Index", 0, 1, Int64.Type),
#"Added Custom" = Table.AddColumn(#"Added Index", "Custom", each try [Timestamp] - #"Added Index"{[Index]-1}[Timestamp] otherwise null, type duration)
in #"Added Custom"
Since you are new to this, I will try to help you out as much as I can, feel free to ask any question.
You can to go into power query (transform data) and create an index column this will give each datetime as unique identifier.
Create a custom column using this code
TimeDiff = if('Sheet1'[Index] <> 1, Sheet1[Timestamp] - LOOKUPVALUE ( Sheet1[Timestamp], Sheet1[Index], Sheet1[Index] - 1 ))
In format, select h:nn:ss and this will display you difference

How to fill the null value with product average in power bi

I have one table with missing value for unit price column. I want fill this in power query with average.
I am able to fill the null with entire column average. But I want to consider the product name column while finding the average. I want group by average to fill the null missing unit price column
Please help me to get some solution
So far I tried with below code :
= if [unit price] = null then List. Average(#"Added Custom"[unit price]) else [unit price]
But here I don't know how to consider the product name while finding the average
My sample data :
I assume you want to fill in the null with the average of the numbers already appearing there, instead of the average of the underlying numbers
addition to your code, in the most simplest form possible:
#"Grouped Rows" = Table.Group(#"PutYourPriorStepNameHere", {"Product Name"}, {
{"data", each _, type table},
{"avg", each List.Average(Table.SelectRows(_, each [Unit Price] <> null)[Unit Price]), type number }
}),
#"Expanded data" = Table.ExpandTableColumn(#"Grouped Rows", "data", {"Unit Price"}, {"Unit Price"}),
#"Added Custom" = Table.AddColumn(#"Expanded data", "Custom", each if [Unit Price]=null then [avg] else [Unit Price])
in #"Added Custom"

how to create a DAX measure / power query process / which returns a value from another row, which has the same date & time

Please find below links to the Fact table and the overview of all tables. I would like to create a DAX measure, or new column in the Fact table ("transactions"), where:
The currency is NOT equal to EUR (e.g. "BTC"),
Giving the value equal to a opposite value of "amount", as given in the row with EUR as currency (e.g. +5. Positive for negative and vice versa),
Where the date and time of the two rows (EUR and non-EUR) have the same values (e.g. 03/11/2021 and 12:28:06)
The "type" = "trade",
In all other cases, I think it would be best to give a value of 0.
In my Fact table screenshot, I manually added the EUR_amt column in Excel to show what I would like to create
I think it's also possible to add the column, then group by time and date, such that the rows with EUR as currency with EUR_amt being 0, would be removed. All using power query. That would be even better.
(The "Currencies" table just uses the distinct values of the "currency" column in the "transactions" table, via PowerQuery. Not relevant for this question I think)
Many thanks in advance!
-YK
Fact table "transactions"
Overview of tables
Calculated Column:
EUR_amt =
IF (
OR ( transactions[type] <> "trade", transactions[currency] = "EUR" ),
0,
- LOOKUPVALUE (
transactions[amount],
transactions[Date], transactions[Date],
transactions[Time], transactions[Time],
transactions[currency], "EUR",
0
)
)
Here's one way to do this using just Power Query and the Advanced Editor
Group by data and time
Generate a custom column for each subtable based on your rules
Expand the subtables, remove those with "0", and re-order the columns
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"type", type text}, {"currency", type text}, {"Date", type date}, {"Time", type time}, {"amount", type number}}),
//Group by Date and Time
#"Grouped Rows" = Table.Group(#"Changed Type", {"Date", "Time"}, {
//Add the Eur amt column to the grouped table
{"EUR_amt", (t)=>let
//determine relevant euro amt for each sub table
eur= t[amount]{List.PositionOf(t[currency],"EUR")},
//add the column to each subtable basaed on conditions
addCol = Table.AddColumn(t, "EUR_amt",
each if [type]= "trade" and [currency]<>"EUR" then -eur else 0)
in
addCol}}),
//Expand the new table
//Filter out the 0's
//reorder the columns
#"Expanded EUR_amt" = Table.ExpandTableColumn(#"Grouped Rows", "EUR_amt", {"type", "currency", "amount", "EUR_amt"}, {"type", "currency", "amount", "EUR_amt"}),
#"Filtered Rows" = Table.SelectRows(#"Expanded EUR_amt", each ([EUR_amt] <> 0)),
#"Reordered Columns" = Table.ReorderColumns(#"Filtered Rows",{"type", "currency", "Date", "Time", "amount", "EUR_amt"})
in
#"Reordered Columns"

How to add a repeating Index or Rank column in Power Query (1,1,2,2,3,3,4,4 etc.)

I have a large table of data (the below image () is a small subsection of the larger dataset) which I need to essentially add an index or rank column that repeats itself (1,1,2,2,3,3,4,4 etc. ). The reason for this is each two rows of data need to be associated with each other so I can properly pivot the attribute and value columns.
EDIT: Picture display
Bring the data into PowerQuery with data .. from table/range...
Add column ... index column ....
click select the index column
transform .. standard .. integer divide ... 2
Sample code if source data is in Table1
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"a", type text}}),
#"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1),
#"Integer-Divided Column" = Table.TransformColumns(#"Added Index", {{"Index", each Number.IntegerDivide(_, 2), Int64.Type}})
in #"Integer-Divided Column"

SUMX over table variable breaks when adding relationships to underlying table

Environment
I have created the two queries in Power Query
"Demo"
let
Source = Table.FromList(List.Random(1000, 20200427),Splitter.SplitByNothing()),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),
#"Split Column by Position" = Table.SplitColumn(#"Changed Type", "Column1", Splitter.SplitTextByPositions({0, 5, 7}, false), {"Month", "Amount"}),
#"Changed Type2" = Table.TransformColumnTypes(#"Split Column by Position",{{"Month", type number}, {"Amount", type number}}),
ConvetToMonthNumber = Table.TransformColumns(#"Changed Type2",{{"Month", each Number.RoundDown( 12 * _) + 1, Int64.Type}})
in
ConvetToMonthNumber
and "MockCal"
let
Source = Table.FromList( List.Repeat({1..4},3), Splitter.SplitByNothing(),{"CalQuart"}),
#"Added Index" = Table.AddIndexColumn(Source, "Month", 1, 1)
in
#"Added Index"
and then in the model I have two bits of DAX
Running Amount =
CALCULATE(
SUM('Demo'[Amount]),
FILTER(ALL('Demo'[Month]), 'Demo'[Month] <= MAX('Demo'[Month]))
)
and
WEIRD Run total =
VAR CalcTable = SUMMARIZE(Demo,Demo[Month],"MonthlyRollingAmounts",[Running Amount])
VAR TotalAmount = SUMX(CalcTable,[MonthlyRollingAmounts])
RETURN IF(ISFILTERED(Demo[Month]), [Running Amount], TotalAmount)
With this I can produce the following visual:
This achieves the desired result: Create a measure that sums up its slices by month.
However, when I create a relationship off this table, the SUMX stops working as expected.
The question is: Why does adding this relationship change the behavior of the SUMX? It doesn't seem to me like it should matter at all. Help is appreciated, this one is really bending my brain.
The reason for this is that in your Running Amount measure you remove any filtering on 'Demo'[Month] but this does not propagate upstream to MockCal (which I'm assuming you're using as the first column in your visual).
In general, you want to do your date filtering on your calendar table instead of your fact table. Try this instead:
Running Amount =
CALCULATE(
SUM('Demo'[Amount]),
FILTER(ALL(MockCal), 'MockCal'[Month] <= MAX('MockCal'[Month]))
)
Your WEIRD Run total should be updated as well to use the calendar table.