Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
KevinSV
Advocate I
Advocate I

How to create a line to balance amounts (charts of accounts)

Hello everyone ! 
I need your help about a project on Power BI Desktop. 
First of all, I extract my data from SQL Server and the table I need is PCA). And all the columns of my table are : ANALYT ; CLIENT ; CODEJOUR ; CREDITS ; DEBITS ; DTECRIT ; ETABL ; EXERCICE ; ID_SOCIETE ; LIB2 ; LIGNE ; NUM_COMPT ; NUM_FAC ; PIECE ; REFGESTION ; SECTION ; SOCIETE.
So after the cleaning, I put some slicers to filter with DTECRIT, SOCIETE. With the filters, I obtain a visual table where I put all the columns i need : 

KevinSV_0-1733234848714.png

As you can see i obtain some amount in credits but nothing in Debits. I want to create a line (on the top of the table) where the TCOMPTE number will begin with 487 and add 0 in function of the length of the other TCOMPTE (in the screen the length is 6 so i want 3 more 0 on my 487) and a balance for the amount (in this exemple, I have 40 061.50 in DCREDITS and 0 in MDEBITS so I want 40 061.50 in MDEBITS). So, I want to put a balance in the column which had the less amount (for example if I have 1000 in Credits and 300 in Debits, SO on the top line with the TCOMPTE beginning with 487, I will have 700 in Debits (the calcul is the difference between the sum of credits and debits when credits > debits or inverse the difference between the sum of debits and credits when debits > credits). 
Is that possible ? Thank you for your help 🙂

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @KevinSV ,

 

First create a calculated column that generates a new TCOMPTE number that starts with “487” and adds the appropriate number of “0's” based on the maximum length of the existing TCOMPTE number.

TCOMPTE_Balance =
"487"
    & REPT ( "0", LEN ( MAXX ( ALL ( PCA ), PCA[TCOMPTE] ) ) - 3 )

vkongfanfmsft_0-1733291258512.png


Then create the following calculation table that contains the data from the original table PCA and a new row for balancing amounts.

BalanceTable = 
UNION(
    SELECTCOLUMNS(
        PCA,
        "TCOMPTE", PCA[TCOMPTE],
        "MDEBITS", PCA[MDEBITS],
        "DCREDITS", PCA[DCREDITS],
        "DTECRIT", PCA[DTECRIT]
    ),
    ROW(
        "TCOMPTE",  MAX(PCA[TCOMPTE_Balance]),
        "MDEBITS", IF(SUM(PCA[DCREDITS]) > SUM(PCA[MDEBITS]), [BalanceAmount], 0),
        "DCREDITS", IF(SUM(PCA[MDEBITS]) > SUM(PCA[DCREDITS]), [BalanceAmount], 0),
        "DTECRIT", MAX(PCA[DTECRIT])
    )
)

vkongfanfmsft_1-1733291275085.png

 

Best Regards,
Adamk Kong

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

3 REPLIES 3
Anonymous
Not applicable

Hi @KevinSV ,

 

First create a calculated column that generates a new TCOMPTE number that starts with “487” and adds the appropriate number of “0's” based on the maximum length of the existing TCOMPTE number.

TCOMPTE_Balance =
"487"
    & REPT ( "0", LEN ( MAXX ( ALL ( PCA ), PCA[TCOMPTE] ) ) - 3 )

vkongfanfmsft_0-1733291258512.png


Then create the following calculation table that contains the data from the original table PCA and a new row for balancing amounts.

BalanceTable = 
UNION(
    SELECTCOLUMNS(
        PCA,
        "TCOMPTE", PCA[TCOMPTE],
        "MDEBITS", PCA[MDEBITS],
        "DCREDITS", PCA[DCREDITS],
        "DTECRIT", PCA[DTECRIT]
    ),
    ROW(
        "TCOMPTE",  MAX(PCA[TCOMPTE_Balance]),
        "MDEBITS", IF(SUM(PCA[DCREDITS]) > SUM(PCA[MDEBITS]), [BalanceAmount], 0),
        "DCREDITS", IF(SUM(PCA[MDEBITS]) > SUM(PCA[DCREDITS]), [BalanceAmount], 0),
        "DTECRIT", MAX(PCA[DTECRIT])
    )
)

vkongfanfmsft_1-1733291275085.png

 

Best Regards,
Adamk Kong

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hi @Anonymous, 
So I have a problem as you can see on the screenshot, I put some slicers for filter with a society and a date (this columns are on the PCA table) : 

KevinSV_0-1733299910216.png


So the table i have looks like this : 

KevinSV_1-1733299985797.png

But when i put your columns (i related your table with the PCA by creating a column which calculate is "Fictive = 1"), I have few columns that don't exists on the society i filter : 

KevinSV_2-1733300124471.png

And the amount is totally different, in my exemple i hope to get on my first line (where the TCOMPTE will begin with 487) an MDEBITS amount of 28 156.22 but i got this results : 

KevinSV_3-1733300227462.png

How can I fix it ? I thought about a selectedvalue but i'm not sure. 
Thanks again ! 

 



Hi everyone,
I try the code of @Anonymous for my project and it works (thank you again).
The problem I have is the amount on the line of 487 is here only without filter so the total. But when I want to filter with a Society, with a date and a "CODEJOUR", the line which equilibrate the amount disapear. 
Here is the measure, i use : 

BalanceAmount =
VAR TotalCredits = CALCULATE(SUM(PCA[CREDITS]))
VAR TotalDebits = CALCULATE(SUM(PCA[DEBITS]))
RETURN
IF(TotalCredits > TotalDebits; TotalCredits - TotalDebits; TotalDebits - TotalCredits)

And the table i create : 
BalanceTable =
VAR BalanceAmountValue = [BalanceAmount]  
RETURN
UNION(
    SELECTCOLUMNS(
        PCA;
        "LIBELLE"; PCA[LIB2];
        "CODEJOUR"; PCA[CODEJOUR];
        "SOCIETE"; PCA[SOCIETE];
        "TCOMPTE"; PCA[NUM_COMPT];
        "MDEBITS"; PCA[DEBITS];
        "DCREDITS"; PCA[CREDITS];
        "DTECRIT"; PCA[DTECRIT]
    );
    ROW(
        "LIBELLE"; "";
        "CODEJOUR"; SELECTEDVALUE(PCA[CODEJOUR]);
        "SOCIETE"; SELECTEDVALUE(PCA[SOCIETE]);
        "TCOMPTE"; "487" & REPT("0"; LEN(MAXX(ALL(PCA); PCA[NUM_COMPT])) - 3);
        "MDEBITS"; IF(BalanceAmountValue > 0; BalanceAmountValue; 0);
        "DCREDITS"; IF(BalanceAmountValue < 0; ABS(BalanceAmountValue); 0);
        "DTECRIT"; SELECTEDVALUE(PCA[DTECRIT])
    )
)
Without filter :
 KevinSV_0-1733319295520.png

With filter : 

KevinSV_1-1733319336667.png

Do someone has an idea ? 

Thanks you for your helping 🙂

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.

Top Solution Authors