I trying to find a way to get the most recent row for each serie in a measurement.
for example:
Assuming the series in results measurement are:
> select series from test_result
results,service=MyService,team=A
result,service=MyService,team=B
test_result,service=MyService,team=C
and the rows in a given time frame are:
> select * from test_result order by time desc
time service team status duration
---- ------- ---- ------ --------
1523370939000000000 MyService A 1 300
1523370940000000000 MyService B 1 300
1523370941000000000 MyService A 1 300
1523370941000000000 MyService C 1 300
1523371748000000000 MyService A 1 300
1523371749000000000 MyService B 1 300
1523371750000000000 MyService B 1 300
1523371754000000000 MyService A 1 300
I would expect the query to return the first, second and fourth rows.
Any help is much appreciated.
Thanks!
Thanks to Katy from Influx Staff who answered the question:
To separate the series, you can add GROUP BY , which will give you
the results separated by series. Then you can add aggregates to your
query, like LAST. For example: SELECT LAST(field_name), from
test_result GROUP BY *
Keep in mind that your fields are also a factor here. You can use *
without specifying a field, but there’s room for error there. It’s
better to specify a field if you know what you need.
Related
I have the following table (4 users, 7 properties, 4 mortgages)
How can I calculate the AVERAGE and MEDIAN of distinct properties per a user?
For example I want to get the following results:
AVG = (2+1+1+3)/4 = 1.75
MEDIAN ... 1/1/2/3 = 1.5
See Picture
I was trying to have smth like the below but it doesn’t work (It says error in calculations)
avgOver
(
sum(distinct_count({Property_id})), [User_id]
)
I am working with a bunch of data for my job creating status reports on the documents that we are working through that we then assign to an area. We decided to use PowerBI as an interactive way to see where everything is at.
Using Power BI Desktop I've created a new table that excludes documents that are not ready for QC but we have several different statuses. Instead of creating a new table for each status type (since some can be grouped together) I would like to create a new column that has the grouped status value's Max for each area. The higher the Status Value the further it is from being complete.
EX:
Record:
Area:
Status Value:
Max Status Value:
152385
A
1
2
354354
B
2
3
131322
B
3
3
132136
A
2
2
213513
A
1
2
351315
B
2
3
If anyone knows how to get the Max Status Value column that would greatly help. I did find another post (https://community.powerbi.com/t5/Desktop/LOOKUPVALUE-return-min-max-of-values-found/td-p/657534) that was similar but I'm still new to DAX and could not figure out how to apply it to my situation.
This post actually helped me answer the question.
https://community.powerbi.com/t5/Power-Query/Maxifs-Power-Query/m-p/1693606
The only difference I made was getting rid of the true/false portion to receive my results. Thus my result was:
Max Status Value =
VAR vMaxVal=
CALCULATE (
MAX ( 'Table'[Status Value] ),
ALLEXCEPT (
'Table',
'Table'[Area]
)
)
RETURN
vMaxVal
Here is my measure :
CREATE MEMBER CURRENTCUBE.[Measures].[ContactNumber] AS
nonempty(
UNORDER(
(UNORDER([Contact].[Contact Id].[Contact Id].MEMBERS)
,{linkmember([Period].[Per Quarter].currentmember,[Period Ending].[Per Quarter]).NextMember : STRTOMEMBER('TAIL([Period Ending].[Per Quarter].[' + [Period].[Per Quarter].currentmember.LEVEL.name +'],1)(0)')}
,{NULL :[Period].[Per Quarter].currentmember}
,[Category].[Category].currentmember)
)
,[MAX_BeginDate]
).count
it gives me how much customers there are in a category at a period
my fact table is liked
contact periodin periodout category
A 25 26 cat1
A 26 27 cat2
A 27 end cat3
B 1 26 cat0
B 26 end cat1
C 1 2 cat2
C 3 4 cat2
C 4 end cat3
And my dimensions :
Period regular by periodin
Period ending regular by periodout
contact regular by contact
category regular by category
So for the 26th, I will have :
cat0 0
cat1 1(B)
cat2 1(A)
cat3 1(C)
If someone think to an obvious improvement...
it tooks me over 1min-1min30 for all 4categories in one day of 2017. There are more than 100 million rows in the table fact. Every customer has at least 1fact. Calendar begins in 2000 and there are 60 million of customers.
Thank you
Regards
Antho
Maybe this is faster:
CREATE MEMBER CURRENTCUBE.[Measures].[ContactNumber] AS
SUM(
UNORDER(
(UNORDER([Contact].[Contact Id].[Contact Id].MEMBERS)
,{linkmember([Period].[Per Quarter].currentmember,[Period Ending].[Per Quarter]).NextMember : STRTOMEMBER('TAIL([Period Ending].[Per Quarter].[' + [Period].[Per Quarter].currentmember.LEVEL.name +'],1)(0)')}
,{NULL :[Period].[Per Quarter].currentmember}
,[Category].[Category].currentmember)
),
IIF(
ISEMPTY([MAX_BeginDate])
,NULL
,1
)
)
I think linkmember is a slow function - is there any alternative you can use?
Thank you for answering,
I beleive I already tried replacing the count function by sum without good results.
But as soon as I go back to work I will try your proposition.
Yes I can replace all linkmember by reconstructing every member with level, current member name and strtomember function. It is something I can also try.
I also have the period of customer entry. I use it to know if the customer is "new" at the current period. And maybe it could be possible not browsing all the period dimension from the begining but from the "in period" of the customer...
I am a heavy user of R who is trying to port some basic calculations to Power BI but unable to get DAX to perform vector/rowwise division.
For example, with this dataset:
Category Value
-------- -----
A 1
A 2
B 3
C 4
I have created a measure: Value Sum = SUM(Table[Value])
and I would like to create a third column: Value Share = Value / Value Sum to get:
Category Value Value Share
-------- ----- -----------
A 1 0.1
A 2 0.2
B 3 0.3
C 4 0.4
The equivalent in R would be :
table$Value.Share = table$value/Value.Sum
I have tried: Value Share = [Value] / [Value Sum] but I ended up with 1s in all Value Share rows. Tried SUMX and CALCULATETABLE functions but I believe I am missing something fundamental. Can anyone help?
Sure! If you're starting out with DAX, I can highly recommend to view one of the video lectures of Alberto Cairo / Marco Russo like https://www.youtube.com/watch?v=klQAZLr5vxA, because your question (and all of DAX difficulty) is primarily about contexts.
As for your question, I think you're looking for
=[Value]/CALCULATE(SUM([Value]); ALL(TableName))
Of course, you can replace SUM([Value]) with a measure name too.
See also http://www.daxpatterns.com/cumulative-total/
try this formula: divide([value],sum([value]))
SNAP of DAX formula
Consider the following table:
Group ID Date
1001 2014-01-05
1001 2014-01-12
1001 2014-01-22
1002 2014-01-15
1002 2014-01-19
How may I calculate the Duration inside a group in days? Duration is defined as Date[k] - Date[0]
e.g.: The desired table should look like:
Group ID Date Duration
1001 2014-01-05 0
1001 2014-01-12 7
1001 2014-01-22 17
1002 2014-01-15 0
1002 2014-01-19 4
I have tried using MINX to return the group's first Date value:
=MINX(FILTER(Table;Table[Group ID] = Table[Group ID]); Table[Date])
But the value returned is the entire table's minimum, as if FILTER is returning the entire table.
It's not clear whether you are trying to produce a Calculated Column or a Measure - you should probably be trying the later but it can be done either way.
The calculated column would simply be:
= [Date] - CALCULATE(MIN(Table[Date]), FILTER(Table, Table[Group_ID] = EARLIER(Table[Group_ID])))
Far more powerful are Measures! You could get this date
[Raw Measure] = MAX(Table[Date]) - CALCULATE(MIN(Table[Date]), ALL(Table[Date]))
This works because the ALL() opens up the filter context on the Date column whilst retaining the filter on the group.
You would need to iterate over this measure as it would object to the total, in this example you could use:
[Iterated Measure] = SUMX(Table, [Raw Measure])
May depend on what you need to do, but...
=MAX(Table[Date]) - MIN(Table[Date]) could theoretically work w/ GroupID on rows?