How to sort the answer based on the timestamp after flatten the matrix in google sheet? - sorting

Hi everyone,
My goal is to flatten the Answers in C4:E7 into one column and then sort it based on the ascending order of the Submission Timestamp, then sort it again from Answer1 to Answer3.
For example in the screenshot above, Student B submit the answers at 2:49:27pm, which is the earliest among 4 students, so his answer should be on the top of the column and start from Answer 1 to Answer 3 then only follow by the answer from Student A and Student D.
I'm using =QUERY(FLATTEN(C4:E7),"Select * where Col1 is not null") now. I'm not sure how to sort it based on timestamp first in this case.
Column I is the expected output.
Hope to get some help on this issue, any help will be greatly appreciated!

Try:
=arrayformula(query(iferror(split(flatten(if(A4:A<>"",B4:B&char(9999)&C4:E,)),char(9999)),),"select Col2 where Col2 !='' order by Col1,Col2",0))
NOTES:
The starting point is:
=arrayformula(if(A4:A<>"",B4:B&char(9999)&C4:E,))
This repeats your 'Submission time' column with each of the 3 answer columns, separated by a character that is unlikely to be used in your data set char(9999) (✏).
Then flatten() puts them in 1 column:
split() is then used on ✏ to get the results into 2 cols, but you'll need iferror() to stop a formula issue working down the sheet.
Then the query() wraps around the result to select col2 (where it's not empty), and sort by Col1,Col2.
Alternative with filter() so you don't need the iferror():
=arrayformula(query(split(flatten(filter(B4:B&char(9999)&C4:E,B4:B<>"")),char(9999)),"select Col2 where Col2 !='' order by Col1,Col2",0))

Related

Small detail when using this function in Google Sheets

Small detail when using =INDEX($A$8:$A$11;MATCH(MAX(SUMIF($B$8:$B$11;$B$8:$B$11));SUMIF($B$8:$B$11;$B$8:$B$11);0))) If the values in column B are all different it returns the correct date value, but if two identical values in column B coincide in different dates then it returns the date of the first value; it does not return the correct date and it keeps the first one that has the repeated value.
Any idea?
p.s This question can be added to this post
Even more easier way:
On E2 Try this =TRANSPOSE(INDEX(QUERY(A1:B," select A, sum(B) group by A Order By sum(B) Desc "),2))
and format the date and currency accordingly.
You can do that easily and differently to get:
1 - Make a helper table to get unique dates, You can use two ways
a) - Use SUMIF Function to get the sum of Expenditure in each unique date Like so =IF(D2="",,SUMIF($A$2:$A,D2,$B$2:$B)) and drag it down.
b) - By using QUERY Function =QUERY(A1:B11," select A, sum(B) group by A Order By sum(B) Desc ")
2 - to get SUM BY DATE OF HIGHEST EXPENDITURE: =MAX(E2:E)
3 - to get DATE BY HIGHEST EXPENDITURE: =INDEX($D$2:$D,MATCH($H$3,$E$2:$E,0),1)
Make a copy of this sheet "make it yours."
Hope that answerd your question.

Sum of only Distinct values in a Column in DAX

I have table[Table 1] having three columns
OrganizationName, FieldName, Acres having data as follows
organizationname fieldname Acres
ABC |F1 |0.96
ABC |F1 |0.96
ABC |F1 |0.64
I want to calculate the sum of Distinct values of Acres
(eg: 0.96+0.64) in DAX.
One of the problems with doing what you want is that many measures rely on filters and not actual table expressions. So, getting a distinct list of values and then filtering the table by those values, just gives you the whole table back.
The iterator functions are handy and operate on table expressions, so try SUMX
TotalDistinctAcreage = SUMX(DISTINCT(Table1[Acres]),[Acres])
This will generate a table that is one column containing only the distinct values for Acres, and then add them up. Note that this is only looking at the Acres column, so if different fields and organizations had the same acreage -- then that acreage would still only be counted once in this sum.
If instead you want to add up the acreage simply on distinct rows, then just make a small change:
TotalAcreageOnDistinctRows = SUMX(DISTINCT(Table1),[Acres])
Hope it helps.
Ok, you added these requirements:
Thank You. :) However, I want to add Distinct values of Acres for a
Particular Fieldname. Is this possible? – Pooja 3 hours ago
The easiest way really is just to go ahead and slice or filter the original measure that I gave you. But if you have to apply the filter context in DAX, you can do it like this:
Measure =
SUMX(
FILTER(
SUMMARIZE( Table1, [FieldName], [Value] )
, [FieldName] = "<put the name of your specific field here"
)
, [Value]
)

DAX - Meassure that sums only the first occurance by group

I'm trying to figure out how to build a measure that sums a total, but only taking the first non-empty row for a user.
For example, my data looks like the below:
date user value
-----------------
1/1/17 a 15
2/1/17 a 12
1/1/17 b null
5/1/17 b 3
I'd therefore like a result of 18 (15 + 3).
I'm thinking that using FIRSTNONBLANK would help, but it only takes a single column, I'm not sure how to give it the grouping - perhaps some sort of windowing is required.
I've tried the below, but am struggling to work out what the correct syntax is
groupby(
GROUPBY (
myTable,
myTable[user],
“Total”, SUMX(CURRENTGrOUP(), FIRSTNONBLANK( [value],1 ))
),
sum([total])
)
I didn't have much luck getting FIRSTNONBLANK or GROUPBY to work exactly how I wanted, but I think I found something that works:
SUMX(
ADDCOLUMNS(
ADDCOLUMNS(VALUES(myTable[User]),
"FirstDate",
CALCULATE(MIN(myTable[Date]),
NOT(ISBLANK(myTable[Value])))),
"FirstValue",
CALCULATE(SUM(myTable[Value]),
FILTER(myTable, myTable[Date] = [FirstDate]))),
[FirstValue])
The inner ADDCOLUMNS calculates the first non-blank date values for each user in the filter context.
The next ADDCOLUMNS, takes that table of users and first dates and for each user sums each [value] that occurred on each respective date.
The outer SUMX takes that resulting table and totals all of the values of [FirstValue].

MDX - How to select one column and sort the returned data

For a SSRS report, I'm trying to return a list of sorted data from a dimension to use with a parameter.
My dimension is [Radio].[Radio NO].[Radio NO] where the last Radio NO is a string.
I can find examples of returning one column while sorting on another but I can't figure out how to sort and return just one column.
Thanks whytheq! Based on your answer, here's what I came up with that works:
SELECT {} ON COLUMNS,
ORDER(
[Radio].[Radio NO].[Radio NO].MEMBERS
,[Radio].[Radio NO].CURRENTMEMBER.MEMBER_CAPTION
,BASC
) On ROWS
FROM [OurCube]
Without seeing the exact structure of your cube / query an avenue you could explore, if you'd like to order alphabetical, is the following
ORDER(
[Radio].[Radio NO].[Radio NO].MEMBERS
,[Radio].[Radio NO].CURRENTMEMBER.MEMBER_CAPTION
,BDESC
)
If you want to order by a measure in your cube, then something like the following:
ORDER(
[Radio].[Radio NO].[Radio NO].MEMBERS
,[Measures].[Profit]
,BDESC
)
This is a possible if you really need to change the column name before hitting SSRS but it has the disadvantage of changing it to a measure:
WITH
MEMBER [Measures].[thisIsTheNewName] AS
[Radio].[Radio NO].CURRENTMEMBER.MEMBER_CAPTION
SELECT
{[Measures].[thisIsTheNewName]} ON COLUMNS,
ORDER(
[Radio].[Radio NO].[Radio NO].MEMBERS
,[Radio].[Radio NO].CURRENTMEMBER.MEMBER_CAPTION
,BASC
) On ROWS
FROM [OurCube];

How to sort rows in "SELECT ... FOR ALL ENTRIES ...", ORDER BY is not accepted

I am selecting a table that has multiple of the same records (same REQUEST_ID) with different VERSION_NO. So I want to sort it descending so I can take the highest number (latest record).
This is what I have...
IF it_temp2[] IS NOT INITIAL.
SELECT request_id
version_no
status
item_list_id
mod_timestamp
FROM ptreq_header INTO TABLE it_abs3
FOR ALL ENTRIES IN it_temp2
WHERE item_list_id EQ it_temp2-itemid.
ENDIF.
So version_no is one of the SELECT field but I want to sort that field (descending) and only take the first row.
I was doing some research and read that SORT * BY * won't work with FOR ALL ENTRIES. But that's just my understanding from reading up.
Please let me know how I can make this work. Thanks
You can simply sort the itab after the select and delete all adjecent duplicates afterwards, if wanted:
SORT it_abs3 BY request_id [ASCENDING] version_no DESCENDING.
DELETE ADJACENT DUPLICATES FROM it_abs3 COMPARE request_id.
Depending on the amount of expected garbage (to be deleted lines) in the itab an SQL approach is better. See Used_By_Already's answer.
If you are using the term "latest" to indicate "the most recent entry", then the field mod_timestamp appears to be relevant and you could use it this way to choose only the most recent records for each request_id.
SELECT
request_id
, version_no
, status
, item_list_id
, mod_timestamp
FROM ptreq_header h
INNER JOIN (
SELECT
request_id
, MAX(mod_timestamp) AS latest
FROM ptreq_header
GROUP BY
request_id
) l
ON h.request_id = l.request_id
AND h.mod_timestamp = l.latest
If you want the largest version_no, then instead of MAX(mod_timestamp) use MAX(version_no)
Just declare the it_abs3 as a sorted table with key that would consist of the columns you want to sort by.
You can also sort the table after the query.
SORT it_abs3 BY ...

Resources