max and min rates for the particular area -lambda query - linq

my table looks like-
Area MaxRate MinRate Quarter year
crossing 1000 800 1 2013
crossing 900 700 2 2013
crossing 800 600 3 2013
crossing 700 500 4 2013
crossing 600 500 1 2012
crossing 550 450 2 2012
crossing 500 400 3 2012
crossing 450 350 4 2012
indroper 2000 1500 1 2013
indroper 1800 1500 2 2013
I want max and min rates for the particular area in current year as -
**Area MaxRate MinRate year**
crossing 1000 500 2013
please help me to find the query in LINQ-lambda notation

You can use GroupBy to do this:
var results = table.GroupBy(row => new { Area = row.Area, Year = row.year })
.Select(g => new {
Area = g.Key.Area,
MaxRate = g.Max(i => i.MaxRate),
MinRate = g.Min(i => i.MinRate),
Year = g.Key.Year
});

Related

Creating Cross Matrix table with values in Power BI DAX

I have the following table (see below), where # Category per Client measure calculates how many MH+SA categories types are per client.
ClientID = 23 has only one category type - "MH". Everyone else has 2 types - "MH","SA".
Diagnosis:
ClientID DiagnosDescription [# Category per Client measure]
22 SA1 2
22 MH1 2
23 MH2 1
24 SA1 2
24 MH3 2
25 SA1 2
25 SA2 2
25 MH4 2
26 SA1 2
26 MH1 2
27 SA1 2
27 MH1 2
28 SA1 2
28 MH2 2
29 SA2 2
29 MH1 2
30 SA2 2
30 MH1 2
31 SA2 2
31 MH2 2
Also, see colored screenshot below:
I need to create the following cross-matrix table in Power BI:
where:
Axis X = MH category type
Axis Y = SA category type
Value = a measure that calculates # unique (unduplicated) clients for each category pair - "SA-MH", where [# Category per Client] = 2
For example,
for SA1-MH1 pair we have 3 clients (ClientID = 22, 26, 27)
for SA1-MH2 pair we have 1 client (ClientID = 28)
for SA1-MH3 pair we have 1 client (ClientID = 24)
for SA1-MH4 pair we have 1 client (ClientID = 25)
for SA2-MH1 pair we have 2 clients (ClientID = 29, 30)
for SA2-MH2 pair we have 1 client (ClientID = 31)
for SA2-MH3 pair we have 0 clients
for SA2-MH4 pair we have 1 client (ClientID = 25)
My measure that calculates # Clients with 2 categories type combinations ("SA"+"MH") is the following:
# Dual Diagnosed Clients = CALCULATE(
DISTINCTCOUNT('Diagnosis'[ClientID]),
FILTER(
'Diagnosis',
'Diagnosis'[# Category per Client] = 2
)
)
But if I paste this measure into my matrix, it shows the total number of unique clients with [# Category per Client] = 2, instead of calculating the total number of unique clients (Category = 2) - but accordingly to each pair - SA1-MH1, SA1-MH2 etc.
Please HELP either to update my measure or any advice (my DAX is only good for entry level tasks)
Create 2 new tables as follows:
MH = SELECTCOLUMNS( FILTER('Table1', LEFT( 'Table1'[DiagnosDescription], 2) = "MH") , "MH", Table1[DiagnosDescription], "Client ID", Table1[ClientID])
SA = SELECTCOLUMNS( FILTER('Table1', LEFT( 'Table1'[DiagnosDescription], 2) = "SA") , "SA", Table1[DiagnosDescription], "Client ID", Table1[ClientID])
Create a relationship on client id.
Create a measure:
Measure = COUNT(MH[Client ID])
Add everything to a matrix.

Calculate shipping costs on the basis of product weight

my math is not so good, but can you guys help me with this
problem statement
Suppose I have 4 books with weights and prices.
Book1, 0.5KG
Book2, 0.8KG
Book3, 1KG
Book4, 0.3KG
I have a base price (shipping cost) based on weight, which is 30 Rs Per 0.5KG.
Now when I select "book 1", the shipping cost will be 30 Rs, but how can I get the shipping cost for book 2,book 3 and book 4?
it's not related to any programming or algorithm , anyways
if
30Rs -> 0.5KG
x -> 0.8Kg
then simply for Book2
x = (30Rs*0.8KG)/0.5KG = 48Rs
similarly for book3 and book4:
book3 = (30Rs*1KG)/0.5KG = 60Rs
book4 = (30Rs*0.3KG)/0.5KG = 18Rs
another way to solve it is if every 30RS corresponds to 0.5KG then by dividing each side by 5 then 6RS corresponds to 0.1KG.
Book2 is 0.8KG which is 8 times the value 0.1KG then it must cost 8 times the value 6RS so 8 * 6 = 48RS similarly for **Book3 and Book4 where
Book3 = 10 * 6 = 60RS
Book4 = 3 * 6 = 18RS
If the pricing is in brackets, that is the cost for 0 - 0.5kg is 30RS, and 0.5 - 1kg is 60RS, then you have to do as follows:
First find how many brackets you have:
weight / bracketSize
For your books, this will be:
Book1: 0.5/0.5 // 1
Book2: 0.8/0.5 // 1.6
Book3: 1/0.5 // 2
Book4, 0.3/0.5 // 0.6
Then, you need to round that value up to the nearest whole number. How you do this will depend on what language you're using, but it's often called Ceiling or ceil:
Book1: Ceiling(1) // 1
Book2: Ceiling(1.6) // 2
Book3: Ceiling(2) // 2
Book4, Ceiling(0.6) // 1
Then multiply by price to get your answer.
Book1: 1 * 30 // 30
Book2: 2 * 30 // 60
Book3: 2 * 30 // 60
Book4, 1 * 30 // 30
In one line:
result = Ceiling(weight / bracketSize) * pricePerBracket

running or cumulative measure

I would appreciate any advises for my problem :
I have a list of stock with daily values (so several stocks and one value per day for each).
I'm trying to do a cumulative margin % on the total portfolio from the beginning of the year as a measure so i have the results on a daily basis.
So by example if the total portfolio value is 100 one day and 102 the day after and 104 the following day, i would like to have a measure with (for these 3 days) 0, 2, 4%.
I have a measure calculating the margin % of the whole portfolio per day (i can't have a column as the data is not portfolio but stock based) :
And what i would like to achieve is the following :
I tried to do a =CALCULATE(sum(dailies[marge_daily_percent_measure]); FILTER(all(dailies);INT(dailies[Date (Year)])=[annee]))
(the filter is to get the current year data) but the sum cannot be applied to the measure (he's looking for a column).
I also tried a TOTALYTD but i then have 2 issues : The Sum still cannot be applied to the measure and i also need the result on a daily basis.
Thanks for any hints.
Assuming your table with stock prices looks like this
Date
Value
30 December 2021
104
31 December 2021
106
03 January 2022
107
04 January 2022
107
05 January 2022
106
06 January 2022
95
07 January 2022
106
10 January 2022
110
I have calculated a Margin measure, DAX below. And cumulative measure using SUMX.
DAX: Margin
Margin =
VAR _SelectedDate =
SELECTEDVALUE ( 'Table'[Date] )
VAR _SelectedValue =
SELECTEDVALUE ( 'Table'[Value] )
VAR _PreviousDate =
CALCULATE ( MAX ( 'Table'[Date] ), 'Table'[Date] < _SelectedDate )
VAR _PreviousValue =
CALCULATE ( SUM ( 'Table'[Value] ), 'Table'[Date] = _PreviousDate )
VAR Margin =
DIVIDE ( _SelectedValue - _PreviousValue, _PreviousValue )
RETURN
Margin
DAX: Margin Cumulative
Cumulative Margin =
VAR _SelectedDate =
SELECTEDVALUE ( 'Table'[Date] )
VAR Cumulative =
CALCULATE (
SUMX ( VALUES ( 'Table'[Date] ), [Margin] ),
'Table'[Date] <= _SelectedDate
)
RETURN
Cumulative
Bear in mind that the final percentage value you get from Cumulative Margin is not the same as the difference from the first value against the last value. In this case, (110-104)/104 = 5.77%. With the Cumulative, I get 6.91%
Output
One way I've built cumulative measures in the past is to do the following logic to filter on date. Assuming you use this in some kind of time-sliced view (like your table, or a linechart), this should only grab the dates on/before 'todays' date for each row.
_Cumulative_ClosedTasks =
CALCULATE (
[_ClosedTasks],
FILTER (
ALL ('Date'[Date]),
'Date'[Date] <= MAX ('Date'[Date])
)
)
([_ClosedTasks] is just a basic SUM metric)
Does this approach work for your data?

Dax measure- sum of percent of total by group with condition

For simplicity sake, I have the following dummy data:
id val
1 5
1 30
1 50
1 15
2 120
2 60
2 10
2 10
My desired output is the following:
id SUM_GT_10%
1 95%
2 90%
SUM_GT_10% can be obtained by the following steps:
Calculate the sum of val for each id
Divide val by 1
sum of 2 if 2 > 10%
using the example data, the sum of val is 100 for id 1 and 200 for id 2, so we would obtain the following additional columns:
id val 1 2
1 5 100 5%
1 30 100 30%
1 50 100 50%
1 15 100 15%
2 120 200 60%
2 60 200 30%
2 10 200 5%
2 10 200 5%
And our final output (step 3) would be sum of 2 where 2> 10%:
id SUM_GT_10%
1 95%
2 90%
I don't care about the intermediate columns, just the final output, of course.
James, you might want to create a temporary table in your measure and then sum its results:
tbl_SumVAL =
var ThisId = MAX(tbl_VAL[id])
var temp =
FILTER(
SELECTCOLUMNS(tbl_VAL, "id", tbl_VAL[id], "GT%",
tbl_VAL[val] / SUMX(FILTER(tbl_VAL, tbl_VAL[id] = ThisId), tbl_VAL[val])),
[GT%] > 0.1
)
return
SUMX(temp, [GT%])
The temp table is basically recreating two steps that you have described (divide "original" value by the sum of all values for each ID), and then leaving only those values that are greater than 0.1. Note that if your id is not a number, then you'd need to replace MAX(tbl_VAL[id]) with SELECTEDVALUE(tbl_VAL[id]).
The final result looks like that -
Also, make sure to set your id field to "Not Summarize", in case id is a number -

Basic Velocity Algorithm?

Given the following dataset for a single article on my site:
Article 1
2/1/2010 100
2/2/2010 80
2/3/2010 60
Article 2
2/1/2010 20000
2/2/2010 25000
2/3/2010 23000
where column 1 is the date and column 2 is the number of pageviews for an article. What is a basic velocity calculation that can be done to determine if this article is trending upwards or downwards for the most recent 3 days?
Caveats, the articles will not know the total number of pageviews only their own totals. Ideally with a number between 0 and 1. Any pointers to what this class of algorithms is called?
thanks!
update: Your data actually already is a list of velocities (pageviews/day). The following answer simply shows how to find the average velocity over the past three days. See my other answer for how to calculate pageview acceleration, which is the real statistic you are probably looking for.
Velocity is simply the change in a value (delta pageviews) over time:
For article 1 on 2/3/2010:
delta pageviews = 100 + 80 + 60
= 240 pageviews
delta time = 3 days
pageview velocity (over last three days) = [delta pageviews] / [delta time]
= 240 / 3
= 80 pageviews/day
For article 2 on 2/3/2010:
delta pageviews = 20000 + 25000 + 23000
= 68000 pageviews
delta time = 3 days
pageview velocity (over last three days) = [delta pageviews] / [delta time]
= 68,000 / 3
= 22,666 + 2/3 pageviews/day
Now that we know the maximum velocity, we can scale all the velocities to get relative velocities between 0 and 1 (or between 0% and 100%):
relative pageview velocity of article 1 = velocity / MAX_VELOCITY
= 240 / (22,666 + 2/3)
~ 0.0105882353
~ 1.05882353%
relative pageview velocity of article 2 = velocity / MAX_VELOCITY
= (22,666 + 2/3)/(22,666 + 2/3)
= 1
= 100%
"Pageview trend" likely refers to pageview acceleration, not velocity. Your dataset actually already is a list of velocities (pageviews/day). Pageviews are non-decreasing values, so pageview velocity can never be negative. The following describes how to calculate pageview acceleration, which may be negative.
PV_acceleration(t1,t2) = (PV_velocity{t2} - PV_velocity{t1}) / (t2 - t1)
("PV" == "Pageview")
Explanation:
Acceleration is simply change in velocity divided by change in time. Since your dataset is a list of page view velocities, you can plug them directly into the formula:
PV_acceleration("2/1/2010", "2/3/2010") = (60 - 100) / ("2/3/2010" - "2/1/2010")
= -40 / 2
= -20 pageviews per day per day
Note the data for "2/2/2010" was not used. An alternate method is to calculate three PV_accelerations (using a date range that goes back only a single day) and averaging them. There is not enough data in your example to do this for three days, but here is how to do it for the last two days:
PV_acceleration("2/3/2010", "2/2/2010") = (60 - 80) / ("2/3/2010" - "2/2/2010")
= -20 / 1
= -20 pageviews per day per day
PV_acceleration("2/2/2010", "2/1/2010") = (80 - 100) / ("2/2/2010" - "2/1/2010")
= -20 / 1
= -20 pageviews per day per day
PV_acceleration_average("2/3/2010", "2/2/2010") = -20 + -20 / 2
= -20 pageviews per day per day
This alternate method did not make a difference for the article 1 data because the page view acceleration did not change between the two days, but it will make a difference for article 2.
Just a link to an article about the 'trending' algorithm reddit, SUs and HN use among others.
http://www.seomoz.org/blog/reddit-stumbleupon-delicious-and-hacker-news-algorithms-exposed

Resources