Tableau Calculated Field Based on Year Ranges - calculated-field

I am working on creating a calculated field to have dynamic text on a Tableau Dashboard. I can get the first line of each block of code to work, as well as all single years (last block of code). I have tried to also create unique groups and also tried flipping so I went from oldest to newest for each category as now I start with the most recent year range. Neither of these fixed the problem that when I try to narrow to a year range that is not the first in the group I get two or more dynamic text fields showing even though only one should be showing.
Example of Problem
**if {FIXED: COUNTD([Year])=9} then "From 2011 to 2019, "**
**ELSEIF {FIXED: COUNTD([Year])=8} AND [Year]!="2011" then "From 2012 to 2019,"**
ELSEIF {FIXED: COUNTD([Year])=8} AND ([Year]!="2019") then "From 2011 to 2018,"
**ELSEIF {FIXED: COUNTD([Year])=7} AND ([Year]!="2011" AND [Year]!="2012") then "From 2013 to 2019,"**
ELSEIF {FIXED: COUNTD([Year])=7} AND ([Year]!="2011" AND [Year]!="2019") then "From 2012 to 2018,"
ELSEIF {FIXED: COUNTD([Year])=7} AND ([Year]!="2018" AND [Year]!="2019") then "From 2011 to 2017,"
**ELSEIF {FIXED: COUNTD([Year])=6} AND ([Year]!="2011" AND [Year]!="2012" AND [Year]!="2013") then "From 2014 to 2019,"**
ELSEIF {FIXED: COUNTD([Year])=6} AND ([Year]!="2012" AND [Year]!="2013" AND [Year]!="2019") then "From 2013 to 2018,"
ELSEIF {FIXED: COUNTD([Year])=6} AND ([Year]!="2012" AND [Year]!="2018" AND [Year]!="2019") then "From 2012 to 2017,"
ELSEIF {FIXED: COUNTD([Year])=6} AND ([Year]!="2017" AND [Year]!="2018" AND [Year]!="2019") then "From 2011 to 2016,"
**ELSEIF {FIXED: COUNTD([Year])=5} AND ([Year]!="2011" AND [Year]!="2012" AND [Year]!="2013" AND [Year]!="2014") then "From 2015 to 2019,"**
ELSEIF {FIXED: COUNTD([Year])=5} AND ([Year]!="2011" AND [Year]!="2012" AND [Year]!="2013" AND [Year]!="2019") then "From 2014 to 2018,"
ELSEIF {FIXED: COUNTD([Year])=5} AND ([Year]!="2011" AND [Year]!="2012" AND [Year]!="2018" AND [Year]!="2019") then "From 2013 to 2017,"
ELSEIF {FIXED: COUNTD([Year])=5} AND ([Year]!="2011" AND [Year]!="2017" AND [Year]!="2018" AND [Year]!="2019") then "From 2012 to 2016,"
ELSEIF {FIXED: COUNTD([Year])=5} AND ([Year]!="2016" AND [Year]!="2017" AND [Year]!="2018" AND [Year]!="2019") then "From 2011 to 2015,"
**ELSEIF {FIXED: COUNTD([Year])=4} AND ([Year]!="2011" AND [Year]!="2012" AND [Year]!="2013" AND [Year]!="2014" AND [Year]!="2015") then "From 2016 to 2019,"**
ELSEIF {FIXED: COUNTD([Year])=4} AND ([Year]!="2011" AND [Year]!="2012" AND [Year]!="2013" AND [Year]!="2014" AND [Year]!="2019") then "From 2015 to 2018,"
ELSEIF {FIXED: COUNTD([Year])=4} AND ([Year]!="2011" AND [Year]!="2012" AND [Year]!="2013" AND [Year]!="2018" AND [Year]!="2019") then "From 2014 to 2017,"
ELSEIF {FIXED: COUNTD([Year])=4} AND ([Year]!="2011" AND [Year]!="2012" AND [Year]!="2017" AND [Year]!="2018" AND [Year]!="2019") then "From 2013 to 2016,"
ELSEIF {FIXED: COUNTD([Year])=4} AND ([Year]!="2011" AND [Year]!="2016" AND [Year]!="2017" AND [Year]!="2018" AND [Year]!="2019") then "From 2012 to 2015,"
ELSEIF {FIXED: COUNTD([Year])=4} AND ([Year]!="2015" AND [Year]!="2016" AND [Year]!="2017" AND [Year]!="2018" AND [Year]!="2019") then "From 2011 to 2014,"
**ELSEIF {FIXED: COUNTD([Year])=3} AND ([Year]!="2011" AND [Year]!="2012" AND [Year]!="2013" AND [Year]!="2014" AND [Year]!="2015" AND [Year]!="2016") then "From 2017 to 2019,"**
ELSEIF {FIXED: COUNTD([Year])=3} AND ([Year]!="2011" AND [Year]!="2012" AND [Year]!="2013" AND [Year]!="2014" AND [Year]!="2015" AND [Year]!="2019") then "From 2016 to 2018,"
ELSEIF {FIXED: COUNTD([Year])=3} AND ([Year]!="2011" AND [Year]!="2012" AND [Year]!="2013" AND [Year]!="2014" AND [Year]!="2018" AND [Year]!="2019") then "From 2015 to 2017,"
ELSEIF {FIXED: COUNTD([Year])=3} AND ([Year]!="2011" AND [Year]!="2012" AND [Year]!="2013" AND [Year]!="2017" AND [Year]!="2018" AND [Year]!="2019") then "From 2014 to 2016,"
ELSEIF {FIXED: COUNTD([Year])=3} AND ([Year]!="2011" AND [Year]!="2012" AND [Year]!="2016" AND [Year]!="2017" AND [Year]!="2018" AND [Year]!="2019") then "From 2013 to 2015,"
ELSEIF {FIXED: COUNTD([Year])=3} AND ([Year]!="2011" AND [Year]!="2015" AND [Year]!="2016" AND [Year]!="2017" AND [Year]!="2018" AND [Year]!="2019") then "From 2012 to 2014,"
ELSEIF {FIXED: COUNTD([Year])=3} AND ([Year]!="2014" AND [Year]!="2015" AND [Year]!="2016" AND [Year]!="2017" AND [Year]!="2018" AND [Year]!="2019") then "From 2011 to 2013,"
**ELSEIF {FIXED: COUNTD([Year])=2} AND ([Year]!="2011" AND [Year]!="2012" AND [Year]!="2013" AND [Year]!="2014" AND [Year]!="2015" AND [Year]!="2016" AND [Year]!="2017") then "From 2018 to 2019,"**
ELSEIF {FIXED: COUNTD([Year])=2} AND ([Year]!="2011" AND [Year]!="2012" AND [Year]!="2013" AND [Year]!="2014" AND [Year]!="2015" AND [Year]!="2016" AND [Year]!="2019") then "From 2017 to 2018,"
ELSEIF {FIXED: COUNTD([Year])=2} AND ([Year]!="2011" AND [Year]!="2012" AND [Year]!="2013" AND [Year]!="2014" AND [Year]!="2015" AND [Year]!="2018" AND [Year]!="2019") then "From 2016 to 2017,"
ELSEIF {FIXED: COUNTD([Year])=2} AND ([Year]!="2011" AND [Year]!="2012" AND [Year]!="2013" AND [Year]!="2014" AND [Year]!="2017" AND [Year]!="2018" AND [Year]!="2019") then "From 2015 to 2016,"
ELSEIF {FIXED: COUNTD([Year])=2} AND ([Year]!="2011" AND [Year]!="2012" AND [Year]!="2013" AND [Year]!="2016" AND [Year]!="2017" AND [Year]!="2018" AND [Year]!="2019") then "From 2014 to 2015,"
ELSEIF {FIXED: COUNTD([Year])=2} AND ([Year]!="2011" AND [Year]!="2012" AND [Year]!="2015" AND [Year]!="2016" AND [Year]!="2017" AND [Year]!="2018" AND [Year]!="2019") then "From 2013 to 2014,"
ELSEIF {FIXED: COUNTD([Year])=2} AND ([Year]!="2011" AND [Year]!="2014" AND [Year]!="2015" AND [Year]!="2016" AND [Year]!="2017" AND [Year]!="2018" AND [Year]!="2019") then "From 2012 to 2013,"
ELSEIF {FIXED: COUNTD([Year])=2} AND ([Year]!="2013" AND [Year]!="2014" AND [Year]!="2015" AND [Year]!="2016" AND [Year]!="2017" AND [Year]!="2018" AND [Year]!="2019") then "From 2011 to 2012,"
**ELSEIF {FIXED: COUNTD([Year])=1} AND [Year]="2019" then "In 2019,"
ELSEIF {FIXED: COUNTD([Year])=1} AND [Year]="2018" then "In 2018,"
ELSEIF {FIXED: COUNTD([Year])=1} AND [Year]="2017" then "In 2017,"
ELSEIF {FIXED: COUNTD([Year])=1} AND [Year]="2016" then "In 2016,"
ELSEIF {FIXED: COUNTD([Year])=1} AND [Year]="2015" then "In 2015,"
ELSEIF {FIXED: COUNTD([Year])=1} AND [Year]="2014" then "In 2014,"
ELSEIF {FIXED: COUNTD([Year])=1} AND [Year]="2013" then "In 2013,"
ELSEIF {FIXED: COUNTD([Year])=1} AND [Year]="2012" then "In 2012,"
ELSEIF {FIXED: COUNTD([Year])=1} AND [Year]="2011" then "In 2011,"**
ELSE "See selected years in filter"
END

Related

Why am I getting error message using SQL in vba code? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Can you please help me make this code run? I am getting an error
Token ( was not valid
con.Open strCon
Set rs = con.Execute("SELECT concat(q.CHSeries, q.CHNumber) as Chassino,q.Model," & _
"Case q.BRANCH when '6' then 'branch 6' when '2' then 'branch 2' Else 'XX' end Branch," & _
"q.ORDER,q.indt DeofInvoice, q.invn Invoice_no, q.customer Customer," & _
"(select coalesce(sum(amnt*ICVL),0) from lbr2 where pcno<>0 and q.order=orno) + (select coalesce(sum(amnt*ICVL),0) from parts2 where pcno<>0 and q.order=orno)+" & _
"(select coalesce(sum(amnt*ICVL),0) from lbr1 where pcno<>0 and q.order=orno) + (select coalesce(sum(amnt*ICVL),0) from parts1 where pcno<>0 and q.order=orno)" & _
"Internal_Sales," & _
"coalesce(case q.InvoiceCredit when 'F' then (q.RetailwithV-q.V) else -1*(q.RetailwithV-q.V)end,0) External_Sales" & _
"FROM (select VIN2, VIN3, PRDT, GNN2, orno, indt, invn, ca30, itot, vx,cd2" & _
"FROM HEADER1 , PARTS1 WHERE BRNN=header1.GNN2 AND ORNO=header1.ORNO union select VIN2, VIN3, PRDT, GNN2, orno, indt, invn, ca30, itot, vx,cd2" & _
"FROM HEADER1, LBR1 WHERE BRNN=header1.GNN2 AND ORNO=header1.ORNO" & _
"Union select VIN2, VIN3, PRDT, GNN2, orno, indt, invn, ca30, itot, vx,cd2" & _
"FROM HEADER2 , PARTS2 WHERE BRNN=header2.GNN2 AND ORNO=header2.ORNO union select VIN2, VIN3, PRDT, GNN2, orno, indt, invn, ca30, itot, vx, cd2" & _
"FROM HEADER2 , LBR2 WHERE BRNN=header2.GNN2 AND ORNO=header2.ORNO) & q (CHSeries, CHNumber, Model, BRANCH, ORDER, indt, invn, customer, RetailwithV, V, InvoiceCredit)" & _
"where indt ='2019-09-30' order by branch")
For iCols = 0 To rs.Fields.Count - 1
Worksheets("Sheet1").Cells(1, iCols + 1).Value = rs.Fields(iCols).Name
Next```
Since you are not adding newline to your sql, you should at least add space after openig or before closing ".
The sql you posted concats into something messed.
for readibility I would suggest:
Dim sql as String
sql = sql & vbCRLF & "SELECT concat(q.CHSeries, q.CHNumber) as Chassino,q.Model, "
sql = sql & vbCRLF & "Case q.BRANCH when '6' then 'branch 6' when '2' then 'branch 2' Else 'XX' end Branch, "
sql = sql & vbCRLF & "q.ORDER,q.indt DeofInvoice, q.invn Invoice_no, q.customer Customer, "
sql = sql & vbCRLF & "(select coalesce(sum(amnt*ICVL),0) from lbr2 where pcno<>0 and q.order=orno) + (select coalesce(sum(amnt*ICVL),0) from parts2 where pcno<>0 and q.order=orno)+ "
sql = sql & vbCRLF & "(select coalesce(sum(amnt*ICVL),0) from lbr1 where pcno<>0 and q.order=orno) + (select coalesce(sum(amnt*ICVL),0) from parts1 where pcno<>0 and q.order=orno) "
sql = sql & vbCRLF & "Internal_Sales, "
sql = sql & vbCRLF & "coalesce(case q.InvoiceCredit when 'F' then (q.RetailwithV-q.V) else -1*(q.RetailwithV-q.V)end,0) External_Sales "
sql = sql & vbCRLF & "FROM (select VIN2, VIN3, PRDT, GNN2, orno, indt, invn, ca30, itot, vx,cd2 "
sql = sql & vbCRLF & "FROM HEADER1 , PARTS1 WHERE BRNN=header1.GNN2 AND ORNO=header1.ORNO union select VIN2, VIN3, PRDT, GNN2, orno, indt, invn, ca30, itot, vx,cd2 "
sql = sql & vbCRLF & "FROM HEADER1, LBR1 WHERE BRNN=header1.GNN2 AND ORNO=header1.ORNO "
sql = sql & vbCRLF & "Union select VIN2, VIN3, PRDT, GNN2, orno, indt, invn, ca30, itot, vx,cd2 "
sql = sql & vbCRLF & "FROM HEADER2 , PARTS2 WHERE BRNN=header2.GNN2 AND ORNO=header2.ORNO union select VIN2, VIN3, PRDT, GNN2, orno, indt, invn, ca30, itot, vx, cd2 "
sql = sql & vbCRLF & "FROM HEADER2 , LBR2 WHERE BRNN=header2.GNN2 AND ORNO=header2.ORNO) "
sql = sql & vbCRLF & "where indt ='2019-09-30' order by branch "
Set rs = con.Execute(sql)

"Data type mismatch in criteria expression"

I'm trying to get data that are equals to and between 2 dates that i type into my textbox! The column of PO_Date is of DateTime. It works perfectly when i hard code the dates into the select statement, for example PO_Date >= #12/6/2013 3:54:15 PM# AND PO_Date <= #1/1/2015 3:54:15 PM# inclusive of the DropDownList2. The error only comes once i try to implement the txtStartDate.Text and txtEndDate.Text! Help is very much appreciated!
oRs.Open("SELECT PO_Date, PO.Vendor FROM PO
WHERE PO_Date >= '#" & TxtStartDate.Text & "00:00:00 AM""#' AND
PO_Date <= '#" & TxtEndDate.Text & "00:00:00 AM""#'
AND Vendor Like '%" & DropDownList2.SelectedValue & "%'", oCnn)
May be you have typo, but you can check:
oRs.Open("SELECT PO_Date, PO.Vendor FROM PO
WHERE PO_Date >= #" & TxtStartDate.Text & " 00:00:00 AM# AND
PO_Date <= #" & TxtEndDate.Text & " 00:00:00 AM#
AND Vendor Like '%" & DropDownList2.SelectedValue & "%'", oCnn)
AM""#' => AM#
"00:00:00 => " 00:00:00

Too few parameters in Access VBA

I am trying to run this SQL statement in VBA and for some reason it says there are too few parameters and that it expected 1. I cannot figure out which line its on. Any help would be greatly appreciated.
strCount = "INSERT INTO MarketSegmentTotals([State Medicaid], [Commercial], [HIX], [MMP], [CMS Part D (CY " & intYear & ")], [CMS Part D (CY " & (intYear + 1) & ")] ) " & _
"SELECT A.cnt, B.cnt, C.cnt, D.cnt, E.cnt, F.cnt " & _
"FROM ( " & _
"SELECT COUNT([FORMULARY ID]) as cnt " & _
"FROM ImportMetricsIDs " & _
"WHERE [Market Segment]= 'State Medicaid' " & _
") AS A " & _
", ( " & _
"SELECT COUNT([FORMULARY ID]) as cnt " & _
"FROM ImportMetricsIDs " & _
"WHERE [Market Segment]= 'Commercial' " & _
") as B " & _
", ( " & _
"SELECT COUNT([FORMULARY ID]) as cnt " & _
"FROM ImportMetricsIDs " & _
"WHERE [Market Segment]= 'HIX' " & _
") AS C " & _
", ( " & _
"SELECT COUNT([FORMULARY ID]) as cnt " & _
"FROM ImportMetricsIDs " & _
"WHERE [Market Segment]= 'MMP' " & _
") AS D "
strCount2 = strCount & _
", ( " & _
"SELECT COUNT([FORMULARY ID]) as cnt " & _
"FROM ImportMetricsIDs " & _
"WHERE [Market Segment]= 'CMS Part D (CY ' & (intYear) & ')'" & _
") AS E " & _
", ( " & _
"SELECT COUNT([FORMULARY ID]) as cnt " & _
"FROM ImportMetricsIDs " & _
"WHERE [Market Segment]= 'CMS Part D (CY ' & (intYear + 1) & ')'" & _
") AS F "
I think you have a problem with quotes in your statement text. Look at this excerpt based on your code when I tested in the Immediate window:
intYear = 2015
? "WHERE [Market Segment]= 'CMS Part D (CY ' & (intYear) & ')'"
WHERE [Market Segment]= 'CMS Part D (CY ' & (intYear) & ')'
That can't be right. And when Access tries to execute the query and sees intYear, it interprets that to be a parameter because the db engine knows nothing about a VBA variable named intYear.
I think it should look like this:
? "WHERE [Market Segment]= 'CMS Part D (CY " & (intYear) & ")'"
WHERE [Market Segment]= 'CMS Part D (CY 2015)'
I encourage you to follow KevenDenen's advice to add Debug.Print strCount2 to the code after it has finished building the strCount2 string. Then you can run the code and view the text of the completed statement in the Immediate window. (You can use Ctrl+g to go to the Immediate window.) It helps tremendously to examine the actual statement your code is asking Access to execute.

VB 2005 syntax error

can someone help me check for the below coding, I have a syntax error.
I may have missed out some syntax.
"WHERE PaymentLedger.JobScheduleGUID = JobSchedule.GUID " & _
"AND JobSchedule.CLID = LabourProfile.CLID " & _
"AND LabourProfile.CLNationality = 'MALAYSIA' " & _
"AND (PaymentLedger.SOCSOEmployerContri > 0 or PaymentLedger.EPFEmployeeContri > 0) " & _
'"AND (PaymentLedger.Credit > 0) " & _
"AND (PaymentLedger.Credit > 0 or PaymentLedger.Debit > 0) " & _
Thanks
Look at these two lines:
'"AND (PaymentLedger.Credit > 0) " & _
"AND (PaymentLedger.Credit > 0 or PaymentLedger.Debit > 0) " & _
1 - remove the ' on the first one
2 - Remove the & _ on the second one

vbtab in reporting services 2008 expression

i am trying to use tabs in reporting service 2008 expression on a particular textbox.
i have tried using three tabs (& vbTab & vbTab & vbTab &) between field name and value for two fields.
=iif(Trim(Fields!Field1.Value) <>"", "FVE1:" & vbTab & vbTab & vbTab & Fields!Field1.Value & VbCrLf,"") & iif(Trim(Fields!Field2.Value) <>"", "RV:" & vbTab & vbTab & vbTab & Fields!Field2.Value,"" )
what i am trying to aceive is some thing like this
FEV1: 3.29
RV: 0.51
But i am unable to achieve this insted it get like below
FEV1: 3.29
RV: 0.51
any help will be much appeiciated.
thanks
If tabs do not work, try right-aligning with non-breaking spaces Chr(160):
(Line-wrapped for legibility)
= Iif(
Trim(Fields!Field1.Value) <> "",
"FVE1:" & Fields!Field1.Value.ToString().PadLeft(5, Chr(160)) & VbNewLine,
""
)
& Iif(
Trim(Fields!Field2.Value) <> "",
"RV:" & Fields!Field1.Value.ToString().PadLeft(5, Chr(160)),
""
)