Hi all and thanks for any help in advance. We're struggling with displaying data in a different format within a table, functioning much like a pivot table.
I've thoroughly reviewed other discussions on this topic but have not been able to resolve this situation. I'll note here that I am not a developer and rely heavily on GPT and Google for coding.
Below is the query we're trying to display. The new table is meant to have different Channels as column headers across the top of the table (which can be pulled from a getChannels query with Leads data being shown in the table for that channel for each month. We would then have another query that showed a breakdown of New Customers for each month by Channel and so on for the other data like CAC, CPL, etc.
WITH Leads AS (
SELECT
Date
,
Channel
,
SUM(Total
) AS Leads
FROM
ProjectID.RS Monthly Metrics
WHERE
Metric Type
= 'Leads'
AND Date
BETWEEN {{dateRange5.value.start}} AND {{dateRange5.value.end}}
AND Company = "Client Name"
GROUP BY
Date
,
Channel
),
NewCustomers AS (
SELECT
Date
,
Channel
,
SUM(Total
) AS New_Customers
FROM
ProjectID.RS Monthly Metrics
WHERE
Metric Type
= 'Customers'
AND Date
BETWEEN {{dateRange5.value.start}} AND {{dateRange5.value.end}}
AND Company = "Client Name"
GROUP BY
Date
,
Channel
),
Expenses AS (
SELECT
Date
,
Company
,
Category
,
Channel
,
Channel Group
,
Group
,
Line Item
,
Location
,
Total
FROM
ProjectID.RS Expenses
WHERE
Company
= "Client Name"
)
SELECT
Leads.Date
,
Leads.Channel
,
Leads.Leads
,
NewCustomers.New_Customers
,
CASE
WHEN Leads.Leads
!= 0 THEN NewCustomers.New_Customers
/ Leads.Leads
ELSE 0
END AS Conv %
,
Expenses.Category
,
Expenses.Channel Group
,
Expenses.Group
,
Expenses.Line Item
,
Expenses.Location
,
Expenses.Total
AS Expense_Total,
CASE
WHEN Leads.Leads
!= 0 THEN Expenses.Total
/ Leads.Leads
ELSE 0
END AS CPL,
CASE
WHEN NewCustomers.New_Customers
!= 0 THEN Expenses.Total
/ NewCustomers.New_Customers
ELSE 0
END AS CAC
FROM
Leads
LEFT JOIN
NewCustomers
ON
Leads.Date
= NewCustomers.Date
AND Leads.Channel
= NewCustomers.Channel
LEFT JOIN
Expenses
ON
Leads.Date
= Expenses.Date
AND Leads.Channel
= Expenses.Channel
AND "Client Name" = Expenses.Company
ORDER BY
Date DESC