Google Data Studio Table: Dividing Data that has 2 different Years - google-data-studio

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

Related

how to find the max number of days for each city?

As you see below, there are many cities and days(period of raining days).
I want to find the max days even if days are the same.
For example if maximum days are 3 and there are two 3 days, then i want to print out two rows.
Possible outputs would be:
Auckland 2013-11-30 2013-11-30 5
Christchurch 2013-11-10 2013-11-50 4
If there are only 5 cities, there might be 5 rows to 10 rows depending on the same value of days.
I want to use SELECT, IF or CASE, MAX or Count functions, as this part is one of the complete, complex code.
Thank you.
SQL Version:
Microsoft SQL Server 2016 (RTM-GDR) (KB4019088) - 13.0.1742.0 (X64) Jul 5 2017 23:41:17 Copyright (c) Microsoft Corporation Developer Edition (64-bit) on Windows Server 2012 R2 Standard 6.3 (Build 9600: ) (Hypervisor)
this is the example data:
create TABLE practice10 (
station VARCHAR(50),
start_date DATE,
end_date DATE,
days INT,
)
INSERT INTO practice10 values ('Auckland','2013-10-5','2013-10-10', 5),
('Auckland','2013-10-15','2013-10-17', 2),
('Auckland','2013-10-20','2013-10-23', 3),
('Manchester','2015-9-1','2013-9-4', 3),
('Manchester','2013-10-3','2013-10-3', 0),
('Manchester','2013-10-20','2013-10-29', 9);
Order days using dense_rank(). Then use top 1 to get days with highest values
select
top 1 with ties *
from
myTable
order by dense_rank() over (partition by station order by days desc)

SQL Server - Pivot multiple fields (Units,Averages, etc.) for the same values (years)

I have a table, like the below, and want to pivot year field and have the other field names (Units/AVGs) show up as row values
Units AVGs Year
---------------------
3 3.667 2015
2 7.000 2016
1 15.000 2017
Convert to something like the below
2015 2016 2017
----------------------------
Units 3 2 1
AVGs 3.667 7.000 15.000
I'm sure there is simple solution to this (using pivots) but the suggested solutions only pivot one field (e.g. Units) for another field (e.g. Year).

Joining different queries into 1 query in MS Access

For the past few days I've been trying everything that I know and everything that I could find on the internet. But I can't seem to figure it out and this turns me banana's.
I have 3 different MS Access Queries.
QUERY 1
SELECT First([Master Data Results 2013 - 2016].[Winner]) AS Player, Count([Master Data Results 2013 - 2016].[Winner]) AS Won
FROM [Master Data Results 2013 - 2016]
GROUP BY [Master Data Results 2013 - 2016].[Winner]
HAVING (((Count([Master Data Results 2013 - 2016].[Winner]))>1));
This query counts every tennis match won by player for a periode of 3 years
QUERY 2
SELECT First([Master Data Results 2013 - 2016].[Loser]) AS Player, Count([Master Data Results 2013 - 2016].[Loser]) AS Lost
FROM [Master Data Results 2013 - 2016]
GROUP BY [Master Data Results 2013 - 2016].[Loser]
HAVING (((Count([Master Data Results 2013 - 2016].[Loser]))>1));
This query counte everyt tennis match lost by player for a periode of 3 years
QUERY 3
INSERT INTO Master ( Lost )
SELECT Winner.*, Loser.Lost
FROM Loser INNER JOIN Winner ON Loser.Player = Winner.Player;
I have created a "Master" table. In this table I want to put the combined results of QUERY1 and QUERY 2.
The Master table looks like this.
So I want al the players that are in QUERY 1 in the Players column in the Master table.
The numbers counted as a result of QUERY 1 in the Won column in the Master table and the numbers counted as a result of QUERY 2 in the Lost column in the Master table.
When I run each query seperatly than it works,but I want to put all this into 1 query for ease of use.
You can use UNION query for combining results of Query 1 and 2, column [Lost] in query 1 = 0, column [Won] in query 2 = 0 and then aggregate this query, using Sum on [Lost] and [Won]. Something like this:
INSERT INTO Master (Player, Won, Lost)
SELECT Player, Sum(Won), Sum(Lost) FROM (
SELECT First([Master Data Results 2013 - 2016].[Winner]) AS Player,
Count([Master Data Results 2013 - 2016].[Winner]) AS Won,
0 AS Lost
FROM [Master Data Results 2013 - 2016]
GROUP BY [Master Data Results 2013 - 2016].[Winner]
HAVING (((Count([Master Data Results 2013 - 2016].[Winner])) > 1))
UNION
SELECT First([Master Data Results 2013 - 2016].[Loser]) AS Player,
0 as Won,
Count([Master Data Results 2013 - 2016].[Loser]) AS Lost
FROM [Master Data Results 2013 - 2016]
GROUP BY [Master Data Results 2013 - 2016].[Loser]
HAVING (((Count([Master Data Results 2013 - 2016].[Loser])) > 1))
)
GROUP BY Player

One to many Relationship in SQL Server Analysis Services

I have these tables:
DimDate (PK: DateKey, other attributes)
FactActivationCodes (PK: ActivationCode, IssuedDateKey (FK to DimDate)
FactExpirations (PK: ActivationCode + ExpirationType, FK: ActivationCode to FactActivationCodes)
I set up measures that count the number of rows in
Issued Count (count of rows in FactActivationCodes)
Expired Count (count of distinct ActivationCodes in FactExpirations)
The idea is that the FactActivationCodes has one activation code, with a date when it was issued. The activation code can get expired year after year (and then renewed) so it would have a row for expiration in FactExpirations (one each year)
I put some test rows in the tables; I put 3 rows in FactActivationCodes (different IssuedDate for each) , and only 2 in FactExpirations. When I browse the cube, and I am looking at the count of Issued on columns, and the Issued Date (dimension) on rows, it looks like this:
Issued Date
January 2008 1
February 2008 1
March 2008 1
But then, when I add the Expired Count, I was hoping to see the 'expired column' count with only the ones that match the 'Activation Code' like so, because of the one to many relationship between the two fact tables:
Issued Date Expired Date
January 2008 1 1
February 2008 1 1
March 2008 1 0
But instead, I a cross join of everything like so, with the totals of expired:
Issued Date Expired Date
January 2008 1 2
February 2008 1 2
March 2008 1 2
April 2008 2
May 2008 2
June 2008 2
And onwards, for every date entry in my Date Dimensions... I guess I'm not doing the relationship correctly... how can I get the expected result?
The answer to use referenced relationship: http://technet.microsoft.com/en-us/library/ms166704.aspx

How to add "missing" columns in a column group in reporting services?

I'm trying to create a report that displays for each months of the year the quantity of goods sold.
I have a query that returns the list of goods sold for each month, it looks something like this :
SELECT Seller.FirstName, Seller.LastName, SellingHistory.Month, SUM(SellingHistory.QuantitySold)
FROM SellingHistory JOIN Seller on SellingHistory.SellerId = Seller.SellerId
WHERE SellingHistory.Year = #Year
GOUP BY Seller.FirstName, Seller.LastName, SellingHistory.Month
What I want to do is display a report that has a column for each months + a total column that will display for each Seller the quantity sold in the selected month.
Seller Name | Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec | Total
What I managed to do is using a matrix and a column group (group on Month) to display the columns for existing data, if I have data from January to March, it will display the 3 first columns and the total. What I would like to do is always display all the columns.
I thought about making that by adding the missing months in the SQL request, but I find that a bit weird and I'm sure there must be some "cleanest" solution as this is something that must be quite frequent.
Thanks.
PS: I'm using SQL Server Express 2008
SELECT Seller.Id,
SUM(CASE WHEN SellingHistory.Month = 'Jan' THEN SellingHistory.QuantitySold END ) AS Jan,
SUM(CASE WHEN SellingHistory.Month = 'Feb' THEN SellingHistory.QuantitySold END ) AS Feb,
...
GROUP BY Seller.Id
You can also use PIVOT(double check syntax, I think the following query is ok, but I haven't worked with transact sql for a while) :
SELECT Seller.Id, Jan, Feb, ...
FROM ...
PIVOT (SUM(SellingHistory.QuantitySold) FOR SellingHistory.Month IN (
[Jan],[Feb],....)) AS t;
To do this in T-SQL you want a PIVOT, there is an example using months midway down the page here.

Resources