 INNER JOIN sites T259 ON T54.site_id = T259.id
 INNER JOIN datasources T55 ON T54.datasource_id = T55.id
 ---------------------------------------------------
+-- OPTIONAL ALL DATASOURCES
+-- Logic pulled from: https://community.tableau.com/docs/DOC-9974
+-- Credit: https://community.tableau.com/people/matthew.coles
 -- Table datasources
 SELECT DISTINCT
 T55.asset_key_id AS "Asset_Key_ID (DS)",

T55.hidden_name AS "Hidden_Name (DS)",
T55.id AS "Id (DS)",
T55.incrementable_extracts AS "Incrementable_Extracts (DS)",
T55.is_hierarchical AS "Is_Hierarchical (DS)",
T55.last_published_at AS "Last_Published_At (DS)",
T55.lock_version AS "Lock_Version (DS)",

T55.size AS "Size (DS)",
T55.state AS "State (DS)",
T55.table_name AS "Table_Name (DS)",
T55.updated_at AS "Updated_At (DS)",
CASE T55.connectable WHEN true THEN false ELSE true END AS "Embedded_In_Workbook (DS)" ,
-- <--- BEGIN OPTIONAL ALL DATASOURCES COLUMNS --->
-- Include to get all Datasources: Published and Embedded
CASE WHEN T55_dc.dbclass = 'sqlproxy' THEN true ELSE false END AS "References_Published_Data_Source (DS)" ,
COALESCE(T49_dc.has_extract,T55.data_engine_extracts) AS "Has_Extract (DS)" ,
-- Published Datasources - Is the Datasource Published Seperately to Tableau Server
CASE T55_dc.dbclass WHEN 'sqlproxy' THEN T55_ds.id ELSE T55.id END AS "Id (DS Published)", -- Published Datasource ID.  For workbooks which use a published datasource, take that ID rather than the Datasource ID referenced by the workbook.
CASE T55_dc.dbclass WHEN 'sqlproxy' THEN T55_ds.data_engine_extracts ELSE T55.data_engine_extracts END AS "Data_Engine_Extracts (DS Published)",
CASE T55_dc.dbclass WHEN 'sqlproxy' THEN T55_ds.refreshable_extracts ELSE T55.refreshable_extracts END AS "Refreshable (DS Published)",
CASE T55_dc.dbclass WHEN 'sqlproxy' THEN T55_ds.incrementable_extracts ELSE T55.incrementable_extracts END AS "Incrementable_Extracts (DS Published)",
CASE T55_dc.dbclass WHEN 'sqlproxy' THEN T55_ds.extracts_refreshed_at ELSE T55.extracts_refreshed_at END AS "Extracts_Refreshed_At (DS Published)",
CASE T55_dc.dbclass WHEN 'sqlproxy' THEN T55_ds.extracts_incremented_at ELSE T55.extracts_incremented_at END AS "Extracts_Incremented_At (DS Published)",
CASE T55_dc.dbclass WHEN 'sqlproxy' THEN T55_ds.name ELSE T55.name END AS "Name (DS Published)",
CASE T55_dc.dbclass WHEN 'sqlproxy' THEN T55_ds.owner_id ELSE T55.owner_id END AS "Owner_ID (DS Published)",
CASE T55_dc.dbclass WHEN 'sqlproxy' THEN T55_ds.project_id ELSE T55.project_id END AS "Project_ID (DS Published)",
CASE T55_dc.dbclass WHEN 'sqlproxy' THEN T55_ds.db_class ELSE T55.db_class END AS "Db_Class (DS Published)",
CASE T55_dc.dbclass WHEN 'sqlproxy' THEN T55_ds.db_name ELSE T55.db_name END AS "Db_Name (DS Published)",
CASE T55_dc.dbclass WHEN 'sqlproxy' THEN T55_ds.table_name ELSE T55.table_name END AS "Table_Name (DS Published)",
CASE T55_dc.dbclass WHEN 'sqlproxy' THEN T55_ds.is_hierarchical ELSE T55.is_hierarchical END AS "Is_Hierarchical (DS Published)",
CASE T55_dc.dbclass WHEN 'sqlproxy' THEN T55_ds.is_certified ELSE T55.is_certified END AS "Is_Certified (DS Published)",
T55_ds.repository_url AS "Repository_Url (DS Published)" --new
-- <--- END OPTIONAL ALL DATASOURCES COLUMNS --->
FROM
datasources T55
-- <--- BEGIN OPTIONAL ALL DATASOURCES JOINS --->
LEFT JOIN (SELECT datasource_id, has_extract FROM data_connections GROUP BY datasource_id, has_extract) as T49_dc ON T55.id = T49_dc.datasource_id -- Obtains Extract information on first level Datasources (T49_dc = embedded data connections)
LEFT JOIN data_connections T55_dc ON T55.id = T55_dc.datasource_id AND T55_dc.dbclass = 'sqlproxy' -- Obtains information on Datasources used by a Workbook where they connect to Published Datasources
LEFT JOIN datasources T55_ds ON T55_dc.dbname = T55_ds.repository_url AND T55_ds.connectable = true -- Published Connectable Datasources for additional info
-- <---  END OPTIONAL ALL DATASOURCES JOINS --->
INNER JOIN data_connections T49 ON T55.id = T49.datasource_id
LEFT JOIN data_connections T49 ON T55.owner_id = T49.id AND data_connections.OWNER_TYPE = 'Datasource'
INNER JOIN datasource_metrics_aggregations T52 ON T55.id = T52.datasource_id
