Esto fue lo que hice para obtener todos los movimientos del Balance de las Sociedades que Necesitaba, primero una vista y despues un query que consuma esa vista.
Mi Vista:
CREATE "COMBINEDBALANCESAP" ( "Account",
"Mes",
"Ano",
"SaldoApertura",
"Debito",
"Credito",
"Saldo",
"Sociedad",
"BaseDatos" ) AS ((((((SELECT
T1."Account",
MONTH(T0."RefDate") AS "Mes",
YEAR(T0."RefDate") AS "Ano",
IFNULL(( SELECT
SUM(T3."Debit" - T3."Credit")
FROM "BD1"."OJDT" T2
LEFT JOIN "BD1"."JDT1" T3 ON T2."TransId" = T3."TransId"
WHERE T3."Account" LIKE T1."Account"
GROUP BY T3."Account" ),
0) AS "SaldoApertura",
SUM(T1."Debit") AS "Debito",
SUM(T1."Credit") AS "Credito",
SUM(CASE WHEN
LEFT(T1."Account",
1) = '2'
THEN T1."Credit" - T1."Debit"
ELSE T1."Debit" - T1."Credit"
END) AS "Saldo",
'Sociedad I' AS "Sociedad",
'BD1' AS "BaseDatos"
FROM "BD1"."OJDT" T0
LEFT JOIN "BD1"."JDT1" T1 ON T0."TransId" = T1."TransId"
GROUP BY T1."Account",
MONTH(T0."RefDate"),
YEAR(T0."RefDate") HAVING SUM(T1."Debit" - T1."Credit") != 0)
UNION ALL (SELECT
T1."Account",
MONTH(T0."RefDate") AS "Mes",
YEAR(T0."RefDate") AS "Ano",
IFNULL(( SELECT
SUM(T3."SYSDeb" - T3."SYSCred")
FROM "BD2"."OJDT" T2
LEFT JOIN "BD2"."JDT1" T3 ON T2."TransId" = T3."TransId"
WHERE T3."Account" LIKE T1."Account"
GROUP BY T3."Account" ),
0) AS "SaldoApertura",
SUM(T1."SYSDeb") AS "Debito",
SUM(T1."SYSCred") AS "Credito",
SUM(CASE WHEN
LEFT(T1."Account",
1) = '2'
THEN T1."SYSCred" - T1."SYSDeb"
ELSE T1."SYSDeb" - T1."SYSCred"
END) AS "Saldo",
'Sociedad II' AS "Sociedad",
'BD2' AS "BaseDatos"
FROM "BD2"."OJDT" T0
LEFT JOIN "BD2"."JDT1" T1 ON T0."TransId" = T1."TransId"
GROUP BY T1."Account",
MONTH(T0."RefDate"),
YEAR(T0."RefDate") HAVING SUM(T1."SYSDeb" - T1."SYSCred") != 0))
UNION ALL (SELECT
T1."Account",
MONTH(T0."RefDate") AS "Mes",
YEAR(T0."RefDate") AS "Ano",
IFNULL(( SELECT
SUM(T3."Debit" - T3."Credit")
FROM "BD3"."OJDT" T2
LEFT JOIN "BD3"."JDT1" T3 ON T2."TransId" = T3."TransId"
WHERE T3."Account" LIKE T1."Account"
GROUP BY T3."Account" ),
0) AS "SaldoApertura",
SUM(T1."Debit") AS "Debito",
SUM(T1."Credit") AS "Credito",
SUM(CASE WHEN
LEFT(T1."Account",
1) = '2'
THEN T1."Credit" - T1."Debit"
ELSE T1."Debit" - T1."Credit"
END) AS "Saldo",
'Sociedad III' AS "Sociedad",
'BD3' AS "BaseDatos"
FROM "BD3"."OJDT" T0
LEFT JOIN "BD3"."JDT1" T1 ON T0."TransId" = T1."TransId"
GROUP BY T1."Account",
MONTH(T0."RefDate"),
YEAR(T0."RefDate") HAVING SUM(T1."Debit" - T1."Credit") != 0))
UNION ALL (SELECT
T1."Account",
MONTH(T0."RefDate") AS "Mes",
YEAR(T0."RefDate") AS "Ano",
IFNULL(( SELECT
SUM(T3."Debit" - T3."Credit")
FROM "BD4"."OJDT" T2
LEFT JOIN "BD4"."JDT1" T3 ON T2."TransId" = T3."TransId"
WHERE T3."Account" LIKE T1."Account"
GROUP BY T3."Account" ),
0) AS "SaldoApertura",
SUM(T1."Debit") AS "Debito",
SUM(T1."Credit") AS "Credito",
SUM(CASE WHEN
LEFT(T1."Account",
1) = '2'
THEN T1."Credit" - T1."Debit"
ELSE T1."Debit" - T1."Credit"
END) AS "Saldo",
'Sociedad IV' AS "Sociedad",
'BD4' AS "BaseDatos"
FROM "BD4"."OJDT" T0
LEFT JOIN "BD4"."JDT1" T1 ON T0."TransId" = T1."TransId"
GROUP BY T1."Account",
MONTH(T0."RefDate"),
YEAR(T0."RefDate") HAVING SUM(T1."Debit" - T1."Credit") != 0))
UNION ALL (SELECT
T1."Account",
MONTH(T0."RefDate") AS "Mes",
YEAR(T0."RefDate") AS "Ano",
IFNULL(( SELECT
SUM(T3."Debit" - T3."Credit")
FROM "BD5"."OJDT" T2
LEFT JOIN "BD5"."JDT1" T3 ON T2."TransId" = T3."TransId"
WHERE T3."Account" LIKE T1."Account"
GROUP BY T3."Account" ),
0) AS "SaldoApertura",
SUM(T1."Debit") AS "Debito",
SUM(T1."Credit") AS "Credito",
SUM(CASE WHEN
LEFT(T1."Account",
1) = '2'
THEN T1."Credit" - T1."Debit"
ELSE T1."Debit" - T1."Credit"
END) AS "Saldo",
'Sociedad V' AS "Sociedad",
'BD5' AS "BaseDatos"
FROM "BD5"."OJDT" T0
LEFT JOIN "BD5"."JDT1" T1 ON T0."TransId" = T1."TransId"
GROUP BY T1."Account",
MONTH(T0."RefDate"),
YEAR(T0."RefDate") HAVING SUM(T1."Debit" - T1."Credit") != 0))
UNION ALL (SELECT
T1."Account",
MONTH(T0."RefDate") AS "Mes",
YEAR(T0."RefDate") AS "Ano",
IFNULL(( SELECT
SUM(T3."SYSDeb" - T3."SYSCred")
FROM "B6"."OJDT" T2
LEFT JOIN "B6"."JDT1" T3 ON T2."TransId" = T3."TransId"
WHERE T3."Account" LIKE T1."Account"
GROUP BY T3."Account" ),
0) AS "SaldoApertura",
SUM(T1."SYSDeb") AS "Debito",
SUM(T1."SYSCred") AS "Credito",
SUM(CASE WHEN
LEFT(T1."Account",
1) = '2'
THEN T1."SYSCred" - T1."SYSDeb"
ELSE T1."SYSDeb" - T1."SYSCred"
END) AS "Saldo",
'Sociedad VI' AS "Sociedad",
'B6' AS "BaseDatos"
FROM "B6"."OJDT" T0
LEFT JOIN "B6"."JDT1" T1 ON T0."TransId" = T1."TransId"
GROUP BY T1."Account",
MONTH(T0."RefDate"),
YEAR(T0."RefDate") HAVING SUM(T1."SYSDeb" - T1."SYSCred") != 0)) WITH READ ONLY
Y este es mi query en power bi:
WITH OACT_UNION AS (
SELECT "AcctCode", "AcctName", "FatherNum", "Levels","FormatCode",'Sociedad I' AS "BaseDatos" FROM "B1"."OACT"
UNION ALL
SELECT "AcctCode", "AcctName", "FatherNum", "Levels","FormatCode",'Sociedad II' AS "BaseDatos" FROM "B2"."OACT"
UNION ALL
SELECT "AcctCode", "AcctName", "FatherNum", "Levels","FormatCode",'Sociedad III' AS "BaseDatos" FROM "B3"."OACT"
UNION ALL
SELECT "AcctCode", "AcctName", "FatherNum", "Levels", "FormatCode" ,'Sociedad IIII' AS "BaseDatos" FROM "B4"."OACT"
UNION ALL
SELECT "AcctCode", "AcctName", "FatherNum", "Levels","FormatCode" ,'Sociedad IV' AS "BaseDatos" FROM "B5"."OACT"
UNION ALL
SELECT "AcctCode", "AcctName", "FatherNum", "Levels", "FormatCode",'Sociedad V' AS "BaseDatos" FROM "B6"."OACT"
)
SELECT
balance."Sociedad",
CASE
WHEN OA."Levels" = 4 THEN UPPER(T5."AcctName")
WHEN OA."Levels" = 7 THEN UPPER(T8."AcctName")
WHEN OA."Levels" = 6 THEN UPPER(T7."AcctName")
ELSE UPPER(T6."AcctName")
END AS "Nivel 1",
CASE
WHEN OA."Levels" = 4 THEN LEFT(T4."FormatCode", 2) || ' - ' || T4."AcctName"
WHEN OA."Levels" = 7 THEN LEFT(T7."FormatCode", 2) || ' - ' || T7."AcctName"
WHEN OA."Levels" = 6 THEN LEFT(T6."FormatCode", 2) || ' - ' || T6."AcctName"
ELSE
LEFT(T5."FormatCode", 2) || ' - ' || T5."AcctName"
END AS "Nivel 2",
CASE
WHEN OA."Levels" = 4 THEN LEFT(T3."FormatCode", 3) || ' - ' || T3."AcctName"
WHEN OA."Levels" = 7 THEN LEFT(T6."FormatCode", 3) || ' - ' || T6."AcctName"
WHEN OA."Levels" = 6 THEN LEFT(T5."FormatCode", 3) || ' - ' || T5."AcctName"
ELSE
LEFT(T4."FormatCode", 3) || ' - ' || T4."AcctName"
END AS "Nivel 3",
CASE
WHEN OA."Levels" = 4 THEN LEFT(T3."FormatCode", 5) || ' - ' || T3."AcctName"
WHEN OA."Levels" = 7 THEN LEFT(T5."FormatCode", 5) || ' - ' || T5."AcctName"
WHEN OA."Levels" = 6 THEN LEFT(T4."FormatCode", 5) || ' - ' || T4."AcctName"
ELSE
LEFT(T3."FormatCode", 5) || ' - ' || T3."AcctName"
END
AS "Nivel 4",
CASE
WHEN OA."Levels" = 4 THEN OA."FormatCode" || ' - ' || OA."AcctName"
WHEN OA."Levels" = 7 THEN T4."FormatCode" || ' - ' || T4."AcctName"
WHEN OA."Levels" = 6 THEN T3."FormatCode" || ' - ' || T3."AcctName"
ELSE
OA."FormatCode" || ' - ' || OA."AcctName"
END
AS "Nivel 5",
CASE
WHEN OA."Levels" = 7 THEN T3."FormatCode" || ' - ' || T3."AcctName"
WHEN OA."Levels" = 6 THEN OA."FormatCode" || ' - ' || OA."AcctName"
ELSE
'N/A'
END
AS "Nivel 6",
CASE
WHEN OA."Levels" = 7 THEN OA."FormatCode" || ' - ' || OA."AcctName"
ELSE
'N/A'
END
AS "Nivel 7",
balance."Account" as "Cuenta",
balance."SaldoApertura" as "SaldoApertura",
balance."Debito" as "Debito Ac",
balance."Credito" as "Credito Ac",
balance."Saldo" as "Saldo Ac",
balance."Mes" AS "MM",
balance."Ano" AS "YYYY",
OA."Levels"
FROM CombinedBalanceSAP balance
INNER JOIN OACT_UNION OA ON OA."AcctCode" = balance."Account" AND OA."BaseDatos" = balance."BaseDatos"
INNER JOIN OACT_UNION T3 ON OA."FatherNum" = T3."AcctCode" AND T3."BaseDatos" = balance."BaseDatos"
INNER JOIN OACT_UNION T4 ON T3."FatherNum" = T4."AcctCode" AND T4."BaseDatos" = balance."BaseDatos"
INNER JOIN OACT_UNION T5 ON T4."FatherNum" = T5."AcctCode" AND T5."BaseDatos" = balance."BaseDatos"
LEFT JOIN OACT_UNION T6 ON T5."FatherNum" = T6."AcctCode" AND T6."BaseDatos" = balance."BaseDatos"
LEFT JOIN OACT_UNION T7 ON T6."FatherNum" = T7."AcctCode" AND T7."BaseDatos" = balance."BaseDatos"
LEFT JOIN OACT_UNION T8 ON T7."FatherNum" = T8."AcctCode" AND T8."BaseDatos" = balance."BaseDatos"
LEFT JOIN OACT_UNION T9 ON T8."FatherNum" = T9."AcctCode" AND T9."BaseDatos" = balance."BaseDatos"
WHERE balance."Account" LIKE '1%' OR balance."Account" LIKE '2%'
ORDER BY T6."AcctCode", T5."AcctCode", T4."AcctCode", T3."AcctCode"
Y en Power Bi estoy usando una objeto visual llamado Financial Reporting Matrix de Profitbase donde en los Rows agrego todos mis niveles y el Unico Value es Saldo Ac.