Dax measure to calculate minimum value with in a group of a complete table - group-by

The ask is to create a measure (not a calculated column using Earlier), to fetch minimum unit price of a product group by year and "UnitofMeasure" and show them as "UnitPriceMin" against complete list of data in separate column as shown below-

You can try with this below measure.
I have considered column "Year", "description" and "unitmeasure" for the grouping. You can add/remove columns as per your necessity.
Considered your table name - "product_details". change it as per your table name.
group_wise_min =
VAR current_row_year = MIN(product_details[year])
VAR current_row_product = MIN(product_details[description])
VAR current_row_unit_measure = MIN(product_details[unitmeasure])
RETURN
CALCULATE(
MIN(product_details[unitprice]),
FILTER(
ALL(product_details),
product_details[year] = current_row_year
&& product_details[description] = current_row_product
&& product_details[unitmeasure] = current_row_unit_measure
)
)
Output will be as below-

Related

How to find the column based on the minimum value and the position

A new column should be added as a function of the condition.
Match Condition
eg : ID 1877
1.) In the id group, the minimum value of tho_cw_diff should be checked and Rail position of thor_pos/CW_pos should be same.
eg : ID 7931
2.) If the rail position is not the same for the minimum tho_cw_diff value, check for the other minimum value in group ID that the rail position thor_pos/CW_po is the same.
eg: ID 8880
3.) If all the rail position is different for the group ID, select the minimum tho_cw_diff value.
According to these three conditions, the new column must be updated.
Sharing screenshot data of the expected outputs below
You can try this one.
VAR ID = [ID]
VAR CV_POS = [CV_POS]
VAR tmpTbl =
CALCULATETABLE(
tbl
,ALL()
,tbl[ID] = ID
,tbl[THO_position] = CV_POS
,tbl[CV_POS] = CV_POS
)
VAR Result =
IF(
ISEMPTY(tmpTbl)
,MIN(tbl[THO_CV_DIFF]) -- may be you can write just [THO_CV_DIFF]. I didn't check
,MINX(tmpTbl,[THO_CV_DIFF])
)
RETURN
Result
I didn't check, so come with a feedback.
By the way, you can add you table by using of following symbols: "|" and "-"
myColumn1
myColumn2
A
1
B
2

Sqlalchemy; count items between two dates over a time period

My postgres DB has a Price table where I store price data for a bunch of products. For each Price object I store when it was created (Price.timestamp), and whenever there is a new price for the product, I create a new Price object, and for the old Price object I store when it ended (Price.end_time). Both times are datetime objects.
Now, I want to count how many Prices there are at over a time period. Easy I thought, so I did the query below:
trunc_date = db.func.date_trunc('day', Price.timestamp)
query = db.session.query(trunc_date, db.func.count(Price.id))
query = query.order_by(trunc_date.desc())
query = query.group_by(trunc_date)
prices_count = query.all()
Which is great, but only counts how many prices were new/created for each day. So what I thought I could do, was to filter so that I would get prices where the trunc_date is between the beginning and the end for the Price, like below:
query = query.filter(Price.timestamp < trunc_date < Price.time_ended)
But apparently you are not allowed to use trunc_date this way. Can anyone help me with how I am supposed to write my query?
Data example:
Price.id Price.timestamp Price.time_ended
1 2022-18-09 2022-26-09
2 2022-13-09 2022-20-09
The query result i would like to get is:
2022-27-09; 0
2022-26-09; 1
2022-25-09; 1
...
2022-20-09; 2
2022-19-09; 2
2022-18-09; 2
2022-17-09; 1
...
2022-12-09; 0
Have you tried separating the conditions inside the filter?
query = db.session.\
query(trunc_date, db.func.count(Price.id)).\
filter(
(Price.timestamp < trunc_date),
(trunc_date < Price.time_ended)
).\
group_by(trunc_date).\
order_by(trunc_date.desc()).\
all()
you can use
trunc_date.between(Price.timestamp, Price.time_ended)
I figured it out.
First I created a date range by using a subquery.
todays_date = datetime.today() - timedelta(days = 1)
numdays = 360
min_date = todays_date - timedelta(days = numdays)
date_series = db.func.generate_series(min_date , todays_date, timedelta(days=1))
trunc_date = db.func.date_trunc('days', date_series)
subquery = db.session.query(trunc_date.label('day')).subquery()
Then I used the subquery as input in my original query, and I was finally able to filter on the dates from the subquery.
query = db.session.query(subquery.c.day, db.func.count(Price.id))
query = query.order_by(subquery.c.day.desc())
query = query.group_by(subquery.c.day)
query = query.filter(Price.timestamp < subquery.c.day)
query = query.filter(Price.time_ended > subquery.c.day)
Now, query.all() will give you a nice list that counts the prices for each day specified in the date_series.

Scala Spark : Creating variable that contains col names for partition

I want to make several columns that will sum over other column partition by several columns. I want to make a variable that contains all the col names for the partition by, in that way my code will be cleaner. It goes like this
var myunit = $"reporttype_code", $"review_code", $"businessgroup_code",
$"company_code", $"grade_code", $"terminationtype_code"
var semesterly = Window.partitionBy(myunit, $semester)
.orderBy($year, $month)
.rowsBetween(Window.unboundedPreceding, Window.currentRow)
var quarterly = Window.partitionBy(myunit, $quarter)
.orderBy($year, $month)
.rowsBetween(Window.unboundedPreceding, Window.currentRow)
test = test.withColumn("turnover_over_semester", sum($"val1").over(semesterly))
.withColumn("turnover_over_quarter", sum($"val1").over(quarterly))
I'm gonna make a lot of columns which includes parition by var unit. But the var raised error : expected ; but , found Is it possible to create the var other way?

PowerBI : Filter by interval

I have a table with columns "date_start" and "date_end".
In the report I want a datepicker that filters the rows where the picked date is between these two columns.
But I have no idea how to do it, and I can't find anything on the internet...
Do you have any idea?
Create a measure and put it to visualization. Here example (2 disconnected table)
PickThis = var __selectedDate = SELECTEDVALUE('Calendars'[Date])
return CALCULATE(COUNTROWS('Employ'), ALL('Employ'[From],'Employ'[To]), 'Employ'[From] <= __selectedDate && ('Employ'[To] >= __selectedDate || ISBLANK('Employ'[To]) ))
dummy data:
Output:

Filter portal for most recently created record by group

I have a portal on my "Clients" table. The related table contains the results of surveys that are updated over time. For each combination of client and category (a field in the related table), I only want the portal to display the most recently collected row.
Here is a link to a trivial example that illustrates the issue I'm trying to address. I have two tables in this example (Related on ClientID):
Clients
Table 1 Get Summary Method
The Table 1 Get Summary Method table looks like this:
Where:
MaxDate is a summary field = Maximum of Date
MaxDateGroup is a calculated field = GetSummary ( MaxDate ;
ClientIDCategory )
ShowInPortal = If ( Date = MaxDateGroup ; 1 ; 0 )
The table is sorted on ClientIDCategory
Issue 1 that I'm stumped on: .
ShowInPortal should equal 1 in row 3 (PKTable01 = 5), row 4 (PKTable01 = 6), and row 6 (PKTable01 = 4) in the table above. I'm not sure why FM is interpreting 1Red and 1Blue as the same category, or perhaps I'm just misunderstanding what the GetSummary function does.
The Clients table looks like this:
Where:
The portal records are sorted on ClientIDCategory
Issue 2 that I'm stumped on:
I only want rows with a ShowInPortal value equal to 1 should appear in the portal. I tried creating a portal filter with the following formula: Table 1 Get Summary Method::ShowInPortal = 1. However, using that filter removes all row from the portal.
Any help is greatly appreciated.
One solution is to use ExecuteSQL to grab the Max Date. This removes the need for Summary functions and sorts, and works as expected. Propose to return it as number to avoid any issues with date formats.
GetAsTimestamp (
ExecuteSQL (
"SELECT DISTINCT COALESCE(MaxDate,'')
FROM Survey
WHERE ClientIDCategory = ? "
; "" ; "";ClientIDCategory )
)
Also, you need to change the ShowInPortal field to an unstored calc field with:
If ( GetAsNumber(Date) = MaxDateGroupSQL ; 1 ; 0 )
Then filter the portal on this field.
I can send you the sample file if you want.