Power BI remove duplicates based on max value - powerbi

I have 2 column; ID CODE, value
Remove duplicates function will remove the examples with the higher value and leave the lower one. Is there any way to remove the lower ones? The result I expected was like this.
I've tried Buffer Table function before but it doesn't work. Seems like Buffer Table just works with date-related data (newest-latest).

You could use SUMMARIZE which can be used similar to a SQL query that takes a MIN value for a column, grouped by some other column.
In the example below, MIN([value]) is taken, given a new column name "MinValue", which is grouped by IDCode. This should return the min value for each IDCode.
NewCalculatedTable =
SUMMARIZE(yourTablename, yourTablename[IDCode], "MinValue", MIN(yourTablename[value]) )
Alternatively, if you want the higher values just replace the MIN function with MAX.

Related

I want to use a calculated field as a constant reference value, but it keep changing with the filter

I have a column Sales and a column date, I want to use the average sales from month 10 (October) as reference to compare with the others months.
I made a calculated field Sales_december like avgIf(Sales,date<parseDate('10/31/2022','MM/dd/yyyy'))
*10/01/2022 is the first date in the date column
I'm using Sales_december as a reference line in a line chart, every time I filter the date to a specific month Sales_december goes to zero. The only solution I found was to create a field Sales_december_const with the average value of Sales_december, Ex.: Sales_december_const =2000
avgOver(ifelse({date} < parseDate('10/31/2022', 'MM/dd/yyyy'), {sales}, NULL), [], PRE_FILTER)
You need to use PRE_FILTER in this position to avoid the filters affecting the value.
PRE_FILTER and PRE_AGG are not supported with avgif, but using ifelse here should work how you have it set up in your calculation.

Failing if condition in Power BI

While I am trying to create a new measure, with condition, I am facing the below error.
I am trying to create a dynamic line based on dates on my line graph. For my case, if Sheet[Date] matches to Test[Date] it should return me a value of 100.
But the error says that a single value for column 'Date' in table Sheet1 cannot be determined.
I also tried to convert the date to week with Weeknum function. But the error still persists.
Does it mean that I can not compare single values in IF condition
If there is a way to compare the dates, kindly guide me.
Sheet[Date] and Test[Date] are columns, not single values.
You need to specify which single values from those columns to compare. For example, you could compare the maximal values of those columns (within the local filter context):
MAX ( Sheet[Date] ) = MAX ( Sheet[Date] )

Sqlite Query to remove duplicates from one column. Removal depends on the second column

Please have a look at the following data example:
In this table, I have multiple columns. There is no PRIMARY KEY, as per the image I attached, there are a few duplicates in STK_CODE. Depending on the (min) column, I want to remove duplicate rows.
According to the image, one stk_code has three different rows. Corresponding to these duplicate stk_codes, value in (min) column is different, I want to keep the row which has minimum value in (min) column.
I am very new at sqlite and I am dealing with (-lsqlite3) to join cpp with sqlite.
Is there any way possible?
Your table has rowid as primary key.
Use it to get the rowids that you don't want to delete:
DELETE FROM comparison
WHERE rowid NOT IN (
SELECT rowid
FROM comparison
GROUP BY STK_CODE
HAVING (COUNT(*) = 1 OR MIN(CASE WHEN min > 0 THEN min END))
)
This code uses rowid as a bare column and a documented feature of SQLite with which when you use MIN() or MAX() aggregate functions the query returns that row which contains the min or max value.
See a simplified demo.

Can't count cells populated by a RegExMatch function

I am attempting to count the sum of a column populated by a RegExMatch function but using a COUNT function, eg =COUNT(F2:F100).
The function that populates a cell with '1' is:
=IF(RegExMatch($E2,"SKU123"),"1","")
I can see '1' appear many times in the column but when I attempt to sum the column I get a zero answer.
Any suggestions for how I perform a better RegExMatch (or alternative) or way to sum the column?
Change your formula from
=IF(RegExMatch($E2,"SKU123"),"1","")
to
=IF(RegExMatch($E2,"SKU123"),1,"")
and will work.
COUNT returns the number of numeric values in a dataset, and you were filling with strings. SO in your version it will always return 0.

powerbi, sum of distinct value and filter for another column

I have the following table and am trying to get the sum of a particular column. First Table
I would like to take distinct values for the 'TrackerID' and when durationConnected is greater than 0, then take the value greater than 0. In the end I would like to get the sum which in this case is 7. (See second table):
Second Table
I tried creating another table by doing the following: AnotherTableTest = SUMMARIZE(journal;journal[PhoneNumber];journal[StartTime];journal[TrackerID];"UniqueCalls";DISTINCTCOUNT(journal[TrackerID]);"TimeConnected";Max(journal[DurationConnected])). This didn't give me the expected result.
I also tried using a measure:
MaxAmount = MAX(journal[DurationConnected])
ActualAmount = SUMX(DISTINCT(journal[TrackerID]);[MaxAmount])
Can anyone help me please?
Try creating a new table like this:
newTable = SUMMARIZECOLUMNS('journal'[TrackerID],
"StartTime", MAX('journal'[StartTime]),
"totalDuration",SUM('journal'[DurationConnected]))
EDIT
When [DurationConnected] is the only value that changes for each [TrackerID], this answer is a bit cleaner. I also added 'journal'[Outbound] on OP's request (see comments).
newTable = SUMMARIZECOLUMNS('journal'[TrackerID],
('journal'[StartTime]),('journal'[Outbound]),
"totalDuration",SUM('journal'[DurationConnected]))