cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Register for our Discovery Summit 2024 conference, Oct. 21-24, where you’ll learn, connect, and be inspired.
Choose Language Hide Translation Bar
Yass
Level III

Plotting a Distribution Tracking Chart Over a Defined Date Range

 

Hi everyone,

I hope you're all doing well ! I have a question and would really appreciate your help.

I've been working on a function to create a NOK tracking chart that shows the NOK percentages over a specific date range. Here's a quick rundown of what i want my function to do :
- I get the start and end dates for the current month.
- Next i creat new table to store the NOK percentages for each date and It goes through each day from the start of the month to today.
- Then it inserts the date and the corresponding NOK percentages into the new table.
- Finally, it creates a line graph showing the NOK percentages over the date range.

I've got the function mostly working, but I'm running into a bit of trouble.

TOP5:get_dates = Function({},
    today_date = Format(Today(), "yyyy-mm-dd");
    start_of_month = Format(Date Increment(Today(), "Month", 0, "Start"), "yyyy-mm-dd");
    evallist({start_of_month, today_date});
);

TOP5:calculate_nok_percentages = Function({dt},

    {start_of_month, today_date} = TOP5:get_dates();

    dt_top5 = dt << Summary(
        Group(:Date, :Nom de fichier, :Ref Produit, :ProductName, :Baie, :ICT_FCT, :Result, :Reason Error, :CodeError),
        Freq("Aucun(e)"),
        Weight("Aucun(e)"),
        invisible
    );

    nameFilesNok = Associative Array(Column(dt_top5, "Nom de fichier") << Get Values) << Get Keys;

    total_nb_total_ict = 0;
    total_nb_total_fct = 0;
    total_nb_nok_ict = 0;
    total_nb_nok_fct = 0;

    For(k = 1, k <= N Items(nameFilesNok), k++,
        FileRows = dt_top5 << get rows where(
            :File Name == nameFilesNok[k] & Num(Char(:Date)) >= Num(start_of_month) & Num(Char(:Date)) <= Num(today_date) & Contains(:File Name, "GOLDEN") == 0
        );

        nb_total_ict = 0;
        nb_total_fct = 0;
        nb_nok_ict = 0;
        nb_nok_fct = 0;

        total_rows = N Items(FileRows);

        For(j = 1, j <= total_rows, j++,
            If(dt_top5:ICT_FCT[FileRows[j]] == "ICT",
                nb_total_ict = nb_total_ict + dt_top5:Nb. de lignes[FileRows[j]];
                If(dt_top5:Result[FileRows[j]] == "NOK",
                    nb_nok_ict = nb_nok_ict + dt_top5:Nb. de lignes[FileRows[j]]
                );
            , dt_top5:ICT_FCT[FileRows[j]] == "FCT",
                nb_total_fct = nb_total_fct + dt_top5:Nb. de lignes[FileRows[j]];
                If(dt_top5:Result[FileRows[j]] == "NOK",
                    nb_nok_fct = nb_nok_fct + dt_top5:Nb. de lignes[FileRows[j]]
                );
            )
        );

        total_nb_total_ict += nb_total_ict;
        total_nb_total_fct += nb_total_fct;
        total_nb_nok_ict += nb_nok_ict;
        total_nb_nok_fct += nb_nok_fct;
    );

    percent_nok_ict = (total_nb_nok_ict / total_nb_total_ict) * 100;
    percent_nok_fct = (total_nb_nok_fct / total_nb_total_fct) * 100;

    Eval List({percent_nok_ict, percent_nok_fct});
);

TOP5:create_nok_tracking_chart = Function({dt},

    {start_of_month, today_date} = TOP5:get_dates();

    dt_nok = New Table("NOK Percentages",
        Add Rows(0),
        New Column("Date", Character),
        New Column("Percent NOK ICT", Numeric),
        New Column("Percent NOK FCT", Numeric)
    );

    For(i = Num(start_of_month), i <= Num(today_date), i++,
        // Correct date formatting
        today_str = Format(Date(i), "yyyy-mm-dd");
        Eval List({start_of_month, today_date}) = Eval List({today_str, today_str});
        result = TOP5:calculate_nok_percentages(dt);

        Insert Into(dt_nok,
            today_str,
            result[1],
            result[2]
        );
    );

    Graph Builder(
        Size( 700, 500 ),
        Show Control Panel( 0 ),
        Variables( X( :Date ), Y( :Percent NOK ICT ), Y( :Percent NOK FCT ) ),
        Elements(
            Line(
                X,
                Y( 1 ),
                Legend( 6 ),
                Line Color( "Red" )
            ),
            Line(
                X,
                Y( 2 ),
                Legend( 8 ),
                Line Color( "Blue" )
            )
        ),
        Legend Position( "Top" )
    );
);


i did get this error message :

/*:
Unresolved name: Date JSL when accessing or evaluating "Date JSL". Date JSL( i ) /*###*/
Yass_0-1722949692680.png



Could anyone provide some guidance or tips on how to improve this ?

Thanks a lot!

1 ACCEPTED SOLUTION

Accepted Solutions
Yass
Level III

Re: Plotting a Distribution Tracking Chart Over a Defined Date Range

You’ll find attached an updated version of the code that works for anyone interested in this subject !

 

 

// Ouvrir le fichier de données
dt = Open( "/D:/Project JMP/Tables/TableDataPY.jmp", invisible );

today_date = Format(Today(), "yyyy-mm-dd");
start_of_month = Format(Date Increment(Today(), "Month", 0, "Start"), "yyyy-mm-dd");
evallist({start_of_month, today_date});

dt_top5 = dt << Summary(
    Group(:Date, :Nom de fichier, :Ref Produit, :ProductName, :Baie, :ICT_FCT, :Result, :Reason Error, :CodeError),
    Freq("Aucun(e)"),
    Weight("Aucun(e)"),
    invisible
);

results_table = New Table( "Results",
    New Column( "Date", Character ),
    New Column( "Percent NOK ICT", Numeric ),
    New Column( "Percent NOK FCT", Numeric ),
    New Column( "Total ICT", Numeric ),
    New Column( "NOK ICT", Numeric ),
    New Column( "Total FCT", Numeric ),
    New Column( "NOK FCT", Numeric )
);

For(day = Num(start_of_month), day <= Num(today_date), day = Date Increment(day, "Day", 1),
    current_date = Format(day, "yyyy-mm-dd");

    FileRows = dt_top5 << get rows where(
        Num(Char(:Date)) == Num(current_date) & Contains(:Nom de fichier, "GOLDEN") == 0
    );

    nb_total_ict = 0;
    nb_total_fct = 0;
    nb_nok_ict = 0;
    nb_nok_fct = 0;

    total_rows = N Items(FileRows);

    For(j = 1, j <= total_rows, j++,
        If(dt_top5:ICT_FCT[FileRows[j]] == "ICT",
            nb_total_ict = nb_total_ict + dt_top5:Nb. de lignes[FileRows[j]];
            If(dt_top5:Result[FileRows[j]] == "NOK",
                nb_nok_ict = nb_nok_ict + dt_top5:Nb. de lignes[FileRows[j]]
            );
        , dt_top5:ICT_FCT[FileRows[j]] == "FCT",
            nb_total_fct = nb_total_fct + dt_top5:Nb. de lignes[FileRows[j]];
            If(dt_top5:Result[FileRows[j]] == "NOK",
                nb_nok_fct = nb_nok_fct + dt_top5:Nb. de lignes[FileRows[j]]
            );
        )
    );

    If(nb_total_ict > 0,
        percent_nok_ict = (nb_nok_ict / nb_total_ict) * 100,
        percent_nok_ict = 0
    );

    If(nb_total_fct > 0,
        percent_nok_fct = (nb_nok_fct / nb_total_fct) * 100,
        percent_nok_fct = 0
    );

    results_table << Add Rows(1);
    row_index = N Rows(results_table);
    results_table:Date[row_index] = current_date;
    results_table:Percent NOK ICT[row_index] = percent_nok_ict;
    results_table:Percent NOK FCT[row_index] = percent_nok_fct;
    results_table:Total ICT[row_index] = nb_total_ict;
    results_table:NOK ICT[row_index] = nb_nok_ict;
    results_table:Total FCT[row_index] = nb_total_fct;
    results_table:NOK FCT[row_index] = nb_nok_fct;
);

results_table << Save( "D:/Project JMP/Tables Resultats NOK percentages/Results.jmp" );

// Fermer toutes les tables ouvertes
Close( dt, "No Save" );
Close( dt_top5, "No Save" );


 

View solution in original post

19 REPLIES 19
Yass
Level III

Re: Plotting a Distribution Tracking Chart Over a Defined Date Range

The problem is in the create_nok_tracking_chart function

I made some changes to simplify the For loop, but unfortunately, it's not working:

current_date = Date(start_of_month);

    While(current_date <= Date(today_date),
        // Correct date formatting
        today_str = Format(current_date, "yyyy-MM-dd");
        
        result = TOP5:calculate_nok_percentages(dt);
        
        Insert Into(dt_nok,
            today_str,
            result[1],
            result[2]
        );
        
        // Increment date by 1 day
        current_date = Date Increment(current_date, "Day", 1);
    ); 

 

jthi
Super User

Re: Plotting a Distribution Tracking Chart Over a Defined Date Range

What does Date() function do?

-Jarmo
Yass
Level III

Re: Plotting a Distribution Tracking Chart Over a Defined Date Range

I used Date() to create a date object from a list of date. In my case i will get all dates from the begining of month to today's and Date() function will ensure that date are in a date format that supports date arithmetic and comparisons.

jthi
Super User

Re: Plotting a Distribution Tracking Chart Over a Defined Date Range

To my knowledge there is no Date() function in JMP (there is << Date() which can be sent to calendar box but it is totally different thing).

-Jarmo
Yass
Level III

Re: Plotting a Distribution Tracking Chart Over a Defined Date Range

Okay I understand. In that case can I use:

current_date = Format(start_of_month, "yyyy-MM-dd");

While ( current_date <= today_date,

    today_str = Format(current_date, "yyyy-MM-dd");
    result = TOP5:calculate_nok_percentages(dt);

    Insert Into (dt_nok,
        today_str
        result[1],
        result[2]
    );

    current_date = Date Increment(current_date, "Day", 1);
);
jthi
Super User

Re: Plotting a Distribution Tracking Chart Over a Defined Date Range

You are mixing numbers (datenums) and strings. I would use much more robust methods of handling dates than your get_dates function as you are converting dates to strings. You should keep them as dates.

TOP5:get_dates = Function({},
    today_date = Format(Today(), "yyyy-mm-dd");
    start_of_month = Format(Date Increment(Today(), "Month", 0, "Start"), "yyyy-mm-dd");
    evallist({start_of_month, today_date});
);

(I have also provided these more robust methods multiple times in my examples to you).

-Jarmo
Yass
Level III

Re: Plotting a Distribution Tracking Chart Over a Defined Date Range

Yes, that’s what I’m trying to do now. I will keep the date format and work on the logic to see if it works.

Thank you and I’m sorry if my questions are repetitive and sometimes seem silly it’s just because I started using JMP and the JSL language two months ago for my internship.

Yass
Level III

Re: Plotting a Distribution Tracking Chart Over a Defined Date Range

I'm really confused. I've tried this method :

Names Default To Here( 1 );

TOP5 = New Namespace(
    "TOP5"
);

TOP5:get_dates = Function({},
    today_date = Format(Today(), "yyyy-mm-dd");
    start_of_month = Format(Date Increment(Today(), "Month", 0, "Start"), "yyyy-mm-dd");
    evallist({start_of_month, today_date});
);

TOP5:calculate_nok_percentages = Function({dt},

    {start_of_month, today_date} = TOP5:get_dates();

    dt_top5 = dt << Summary(
        Group(:Date, :Nom de fichier, :Ref Produit, :ProductName, :Baie, :ICT_FCT, :Result, :Reason Error, :CodeError),
        Freq("Aucun(e)"),
        Weight("Aucun(e)"),
        invisible
    );

    nameFilesNok = Associative Array(Column(dt_top5, "Nom de fichier") << Get Values) << Get Keys;

    total_nb_total_ict = 0;
    total_nb_total_fct = 0;
    total_nb_nok_ict = 0;
    total_nb_nok_fct = 0;

    For(k = 1, k <= N Items(nameFilesNok), k++,
        FileRows = dt_top5 << get rows where(
            :File Name == nameFilesNok[k] & Num(Char(:Date)) >= Num(start_of_month) & Num(Char(:Date)) <= Num(today_date) & Contains(:File Name, "GOLDEN") == 0
        );

        nb_total_ict = 0;
        nb_total_fct = 0;
        nb_nok_ict = 0;
        nb_nok_fct = 0;

        total_rows = N Items(FileRows);

        For(j = 1, j <= total_rows, j++,
            If(dt_top5:ICT_FCT[FileRows[j]] == "ICT",
                nb_total_ict = nb_total_ict + dt_top5:Nb. de lignes[FileRows[j]];
                If(dt_top5:Result[FileRows[j]] == "NOK",
                    nb_nok_ict = nb_nok_ict + dt_top5:Nb. de lignes[FileRows[j]]
                );
            , dt_top5:ICT_FCT[FileRows[j]] == "FCT",
                nb_total_fct = nb_total_fct + dt_top5:Nb. de lignes[FileRows[j]];
                If(dt_top5:Result[FileRows[j]] == "NOK",
                    nb_nok_fct = nb_nok_fct + dt_top5:Nb. de lignes[FileRows[j]]
                );
            )
        );

        total_nb_total_ict += nb_total_ict;
        total_nb_total_fct += nb_total_fct;
        total_nb_nok_ict += nb_nok_ict;
        total_nb_nok_fct += nb_nok_fct;
    );

    percent_nok_ict = (total_nb_nok_ict / total_nb_total_ict) * 100;
    percent_nok_fct = (total_nb_nok_fct / total_nb_total_fct) * 100;

    Eval List({percent_nok_ict, percent_nok_fct});
);

TOP5:calculate_nok_percentages_over_time = Function({dt},

    {start_of_month, today_date} = TOP5:get_dates();

    dt_results = New Table("NOK Percentages Over Time",
        New Column("Date", Numeric),
        New Column("Percent_NOK_ICT", Numeric),
        New Column("Percent_NOK_FCT", Numeric)
    );

    // Set the format for the Date column
    Column(dt_results, "Date") << Data Type(
        Numeric,
        Format( "Format Pattern", "<YYYY>-<MM>-<DD>", 10 ),
        Input Format( "Format Pattern", "<YYYY>-<MM>-<DD>" ),
        Format( "Date Abbrev", 20 )
    ) << Set Field Width(10);

    date = today_date;

    While(Num(Char(date)) >= Num(start_of_month),
        {percent_nok_ict, percent_nok_fct} = TOP5:calculate_nok_percentages_for_date(dt, date);

        dt_results << Add Rows(
           {Format(date, "yyyy-mm-dd"), percent_nok_ict, percent_nok_fct}
        );

        date = Date Increment(date, "day", -1);
    );

    dt_results;
);

TOP5:calculate_nok_percentages_for_date = Function({dt, specific_date},

    {start_of_month, today_date} = TOP5:get_dates();

    dt_top5 = dt << Summary(
        Group(:Date, :Nom de fichier, :Ref Produit, :ProductName, :Baie, :ICT_FCT, :Result, :Reason Error, :CodeError),
        Freq("Aucun(e)"),
        Weight("Aucun(e)"),
        invisible
    );

    nameFilesNok = Associative Array(Column(dt_top5, "Nom de fichier") << Get Values) << Get Keys;

    total_nb_total_ict = 0;
    total_nb_total_fct = 0;
    total_nb_nok_ict = 0;
    total_nb_nok_fct = 0;

    For(k = 1, k <= N Items(nameFilesNok), k++,
        FileRows = dt_top5 << get rows where(
            :File Name == nameFilesNok[k] & Num(Char(:Date)) >= Num(start_of_month) & Num(Char(:Date)) <= Num(specific_date) & Contains(:File Name, "GOLDEN") == 0
        );

        nb_total_ict = 0;
        nb_total_fct = 0;
        nb_nok_ict = 0;
        nb_nok_fct = 0;

        total_rows = N Items(FileRows);

        For(j = 1, j <= total_rows, j++,
            If(dt_top5:ICT_FCT[FileRows[j]] == "ICT",
                nb_total_ict = nb_total_ict + dt_top5:Nb. de lignes[FileRows[j]];
                If(dt_top5:Result[FileRows[j]] == "NOK",
                    nb_nok_ict = nb_nok_ict + dt_top5:Nb. de lignes[FileRows[j]]
                );
            , dt_top5:ICT_FCT[FileRows[j]] == "FCT",
                nb_total_fct = nb_total_fct + dt_top5:Nb. de lignes[FileRows[j]];
                If(dt_top5:Result[FileRows[j]] == "NOK",
                    nb_nok_fct = nb_nok_fct + dt_top5:Nb. de lignes[FileRows[j]]
                );
            )
        );

        total_nb_total_ict += nb_total_ict;
        total_nb_total_fct += nb_total_fct;
        total_nb_nok_ict += nb_nok_ict;
        total_nb_nok_fct += nb_nok_fct;
    );

    percent_nok_ict = (total_nb_nok_ict / total_nb_total_ict) * 100;
    percent_nok_fct = (total_nb_nok_fct / total_nb_total_fct) * 100;

    Eval List({percent_nok_ict, percent_nok_fct});
);


and when i can my fonction in :

TOP5:Do = Function ({nb_date1, nb_date2},
	//Ouverture de la table de données
	dt = Open( "/D:/Project JMP/Tables/TableData PY.jmp", invisible );

	// Calculate NOK percentages
	{percent_nok_ict, percent_nok_fct} = TOP5:calculate_nok_percentages(dt);

	// Call the new function to get NOK percentages over time
	dt_results = TOP5:calculate_nok_percentages_over_time(dt);

	// Display the results table
	dt_results << Show Window;


I keep getting an error every time.

What I want to do is create a function that calculates the NOK percentages like the second function TOP5:calculate_nok_percentages_over_time . The difference is that this function will calculate the NOK percentage between today's date and the start of the month, then from yesterday to the start of the month, and so on, until it reaches the start of the month. The results should be stored in a new table so I can use them to create graphs.

 

 

jthi
Super User

Re: Plotting a Distribution Tracking Chart Over a Defined Date Range

What is the error you are getting?

-Jarmo