How do you count Distinct Customers and group by first month seen? - google-data-studio

I have a series of customers over the course of the year that I need to group into two categories: New and Reoccurring.
The data roughly looks like this:
ID
Date Seen
1
August 1, 2022
2
August 3, 2022
2
July 1, 2022
2
June 1, 2022
3
July 1, 2022
3
August 1, 2022
New customers would show up on the month that we see their first record. Reoccurring would have more than 1 month logged with us.
How would I count these two groups?
So as an example above, I'd expect to see:
Month
Trial
Returning
August
1
2
July
1
1
June
1
0
Edit: Per request here's a link to a Data Studio report with mock data.

Added the solution to your dashboard.
created a blend and used a calculated field to pivot the data
CASE WHEN Month (Table 1)=Month (Table 2) THEN 'TRIAL' ELSE 'RETURNING' END

Related

Select value of first row where column value matches

I have two sheets in the Google sheet with the following pattern and data
raw sheet
Projects
start date
end date
status
Proj1
July 1, 2021
July 10, 2022
In Progress
Proj2
July 11, 2021
July 20, 2022
Done
Proj1
July 21, 2021
July 25, 2022
Done
and another sheet
project analysis
Projects
start date
end date
status
Time taken (days)
Proj1
July 1, 2021
July 25, 2022
Done
24
Proj2
July 11, 2021
July 20, 2022
Done
10
The values in the project analysis has been manually field, but I want it to automate as follow
set start date in project analysis to the start date column of the first entry from raw sheet
set end date in project analysis to the end date column of the first entry from raw sheet where status is Done
How can I accomplish this in Google sheet?
try:
=ARRAYFORMULA(QUERY({A:C, B:C*1},
"select Col1,min(Col2),max(Col3),'Done',max(Col5)-min(Col4)
where Col1 is not null
group by Col1
label min(Col2)'start date',max(Col3)'end date','Done''status',max(Col5)-min(Col4)'time taken in days'", 1))
In excel ---
Formula in Column B
=VLOOKUP(A13,$A$2:$B$4,2,FALSE)
Array formula in Column C
=INDEX($C$2:$C$4,MATCH(1,(A13=$A$2:$A$4)*("Done"=$D$2:$D$4),0))
Refer INDEX and MATCH with multiple criteria
Formula in Column D
=IF(NOT(ISNA(C13)),"Done","In Progress")
start date:
=ArrayFormula(IF(A2:A="",,VLOOKUP(A2:A,raw!A:B,2,FALSE)))
end date:
=ArrayFormula(IF(A2:A="",,VLOOKUP(A2:A,FILTER(raw!A:C,raw!D:D="Done"),3,FALSE)))

How do I get last 12 months of data based on available data in SQL Server

I have a table test with columns sales and date. The requirement is to get the last available 12 months of data without the current month. For example say that there is data available till 2010 march 5th. The query needs to fetch data from 2009 march till 2010 feb. what would be the where clause on date be in SQL Server
SELECT Sales, Date FROM Test
WHERE Date BETWEEN
DATEADD(m,-12,DATEADD(m, DATEDIFF(m, 0, '05/Mar/2010'), 0))
AND
DATEADD(m, 0,DATEADD(m, DATEDIFF(m, 0, '05/Mar/2010'), 0)) - 1

Google Data Studio Table: Dividing Data that has 2 different Years

I need to produce a table that has Quotes win%. The formula is #won divide by #sent.
My problem is, there are quotes that are won within a year but were sent in different years.
(My data comes from BigQuery)
The data looks like this:
Sale Sent Won
sale1 2019 2020
sale2 2019 2020
sale3 2016 2017
sale4 2017 2019
sale5 2020 2020
sale6 2020 2020
sale7 2018 2018
sale8 2016 2016
sale9 2015 2016
sale10 2016 2017
sale11 2016 2018
sale12 2018 2019
I'd like to be able to create a table in data studio like this:
Year SENT WON WIN%
2016 4 2 50%
2017 1 2 200%
2018 2 2 100%
2019 2 2 100%
2020 2 4 200%
I would love to see if this is possible in google data studio. Any suggestion is highly appreciated.
Added a Google Data Studio Report to demonstrate, as well as a GIF showing the process below.
One approach is to restructure the Data at the Data Set and use Calculated Fields in a Table:
1) Data Transformation
The data needs to be transformed from the current Wide structure to a Long data structure. One way it can be achieved in Google Sheets is by using the formula below (Sheet1 represents the input sheet; consult embedded Google Sheet for clarification):
=ArrayFormula(QUERY({
{Sheet1!A:A,IF(LEN(Sheet1!A:A),"Sent",""),Sheet1!B:B};
{Sheet1!A:A,IF(LEN(Sheet1!A:A),"Won",""),Sheet1!C:C}
},"Select * Where Col3 is not null Label Col2 'Dimension', Col3 'Year'",1))
2) Table
- Dimension: Year
- Sort: Year in Ascending order
- Metrics: Add the 3 calculated fields below:
3) Calculated Fields
The formulas below create the metrics used in the Table above (Formula 3.1 and 3.2 need to be added at the Data Source-level, while 3.3 can be added at the Chart-level if required):
3.1) SENT
COUNT(CASE
WHEN REGEXP_MATCH(Dimension, "Sent") THEN Year
ELSE NULL END)
3.2) WON
COUNT(CASE
WHEN REGEXP_MATCH(Dimension, "Won") THEN Year
ELSE NULL END)
3.3) WIN%
WON / SENT

SQL Query-Multiple date ranges

I want to fetch some data from DB by giving multiple date ranges. Example,in February I want to get weekly report from a table in this order Feb 01 to 07, Feb 07 to 14, Feb 14 to 21, Feb 21 to 28 and Feb 28 to Mar 01. In DB the records are stored in a daily wise not in weekly wise. I want to cluster it as weekly wise and calculate sum then show the result. Please help me if you know this case.
For clear cut view, consider 3 tables & its columns.
Table A:id,timestamp (comment-data is inserted daily)
Table B:id,fruits
Table C:id,fruits_type
Result:
fruits_type count(id) timestamp
apple 3 01-02-2016 to 07-02-2016
orange 5 01-02-2016 to 07-02-2016
pineapple 8 01-02-2016 to 07-02-2016
apple 4 07-02-2016 to 14-02-2016
orange 5 07-02-2016 to 14-02-2016
Conditions:id should match among 3 tables;fetch data by providing group by fruits_type and timestamp should be in weekly wise.
Please help if you know this
To get the sum of all values between two dates you would do it like this:
SELECT SUM(Column1)
FROM Table1
WHERE Date1 BETWEEN '2/1/2016' AND Date1 <'2/7/2016'
If you want to make it more flexible and have the query get the last week's sum you can use the DATEADD function to lag by one week:
SELECT SUM(Column1)
FROM Table1
WHERE Date1 BETWEEN DATEADD(week, -1, GETDATE()) AND Date1 < GETDATE()
If you want the result set to include a row for each week, you can use UNION to merge the queries.

filter for multiple record based on latest timestamp

I have the following table:
Record Created Name Group
1 July 23, 2015 John Group 1
2 July 21, 2015 April Group 1
3 April 4, 2015 John Group 1
How do you filter for the latest distinct record? In this example, I would expect to get record 1 and 3? My code:
Model.objects.filter(Group='Group 1').latest('Created')
only grabs record 1.
latest return an object (row) that is, well, "latest" based on specified field provided as argument.
If you need several records (rows) then you need to get a QuerySet.
Try:
YourModel.objects.filter(Group='Group 1').order_by('-Created')[:2]
this should give you rows 1 and 3 as you expect.

Resources