MacGyver Technique for Dynamic Number of Grouped Subtotals/Subfoots
By David Smith
________________________________________
There are several presentations on the MacGyver Tips and Techniques page discussing moving groups of subtotals to the bottom of a report, but they deal with a known set of grouped values that are hard coded. And what if the number of sort groupings is dynamic and greater than what you may want to manually code? This technique may be of help.
Our business group needed a purchase report that:
1) List detail values (date, invoice#, qty, extend cost, etc.) sorted by region, vendor, and product type
2) At each change in vendor, display on one line beneath the detail the total sales tax for all purchases from the vendor for the period selected as free text.
3) Under the sales tax subtotal line, display a subtotal of the extended cost for each product type purchased from the vendor for the period selected. The number of lines is dynamic and could range from 1 to 100 based on the product types purchased during the as free text.
4) On one line, under the product type subtotals, display the Total Extended cost for the Vendor for all detail lines as free text.
After several "Almost, but no cigar" attempts, I attended the recent MacGyver Webinar, and the light came on.
Use of the MacGyver technique and the new SET DROPBLNKLINES=ON command syntax provided new in 7.6.11 met our business requirement.
Using the CAR database to mimic what our business group needed, I am presenting the output example in HTML format and the underlying fex code.
MGTEST.FEX
-* Set up MacGyver master and data file.
-* Build MacGyver MFD
FILEDEF MCMAS DISK MCGYV.MAS
-RUN
-WRITE MCMAS FILENAME=mcgyv, SUFFIX=FIX , $
-WRITE MCMAS SEGMENT=ONE, SEGTYPE=S0, $
-WRITE MCMAS FIELDNAME=CONTROL, USAGE=A1, ACTUAL=A1, $
-WRITE MCMAS SEGMENT=TWO, SEGTYPE=S0, PARENT=ONE, OCCURS=VARIABLE, $
-WRITE MCMAS FIELDNAME=CHAR1, USAGE=A1, ACTUAL=A1, $
-WRITE MCMAS FIELDNAME=CTR, ALIAS=ORDER, USAGE=I4, ACTUAL=I4, $
-WRITE MCMAS DEFINE REPORT_NUMBER/I4 WITH CHAR1=CTR; $
-* Build MacGyver FTM. Set for count of 2
FILEDEF MCGYV DISK MCGYV.FTM
-RUN
-WRITE MCGYV XAB
-RUN
-*********************************************************************
SET NODATA = ' '
JOIN CONTROL WITH BODYTYPE IN CAR TO UNIQUE CONTROL IN MCGYV AS J2
-* If CTR eq 1 display in detail, if CTR eq 2 use for subfoots.
DEFINE FILE CAR
CONTROL /A1 WITH BODYTYPE='X';
CARX /A40 =IF CTR EQ 1 THEN CAR ELSE ' ';
SORTCTYCAR/A35 =IF CTR EQ 1 THEN COUNTRY ELSE
IF CTR EQ 2 THEN CAR;
SORTMODEL /A30 =IF CTR EQ 1 THEN MODEL ELSE ' ';
SLS_DTL /A15 =IF CTR EQ 2 THEN ' ' ELSE PTOA(SALES,'(P12.2CM)','A15');
SLS_SF /P12.2CM =SALES;
DCOST_DTL /A15 =IF CTR EQ 2 THEN ' ' ELSE PTOA(DEALER_COST,'(P12.2CM)','A15');
DCOST_SF /P12.2CM =DEALER_COST;
END
-* Command to remove blank lines above subfoots
SET DROPBLNKLINE=ON
TABLE FILE CAR
HEADING
"MacGyver Technique with Dynamic Subtotals/SubFoots"
SUM
CARX AS 'Car'
SORTMODEL AS 'Model'
SLS_DTL AS 'Sales'
DCOST_DTL AS 'Cost'
BY COUNTRY NOPRINT
BY CTR NOPRINT
BY SORTCTYCAR NOPRINT
BY SORTMODEL NOPRINT
ON COUNTRY SUBHEAD
"1 Country:
ON CTR SUBFOOT
"Total Dealer Cost: WHEN CTR EQ 1
ON CTR SUBFOOT
"Total sales for WHEN CTR EQ 2
ON SORTCTYCAR SUBFOOT
"Subtotal Sales for WHEN CTR EQ 2
WHERE COUNTRY EQ 'ITALY'
ON TABLE NOTOTAL
ON TABLE SET PAGE NOLEAD
ON TABLE PCHOLD FORMAT HTML
ON TABLE SET STYLE *
GRID=ON,BORDER=OFF,SQUEEZE=ON,SIZE=8,$
TYPE=HEADING,JUSTIFY=CENTER,$
TYPE=TITLE,JUSTIFY=CENTER,$
TYPE=DATA,COLUMN=SLS_DTL ,JUSTIFY=RIGHT,WHEN=CTR EQ 1,$
TYPE=DATA,COLUMN=DCOST_DTL,JUSTIFY=RIGHT,WHEN=CTR EQ 1,$
ENDSTYLE
END
Notes:
• This technique works for HTML, PDF, and EXL2K formats. There is an open case for Active (AHTML) format.
• This technique can work in earlier versions of WebFocus (pre 7.6.10 and earlier) but you will have blank data rows above the subfoots.
• Go to MacGyver Techniques for all of the Tips and Technique documents
• Read MacGyver Technique Introduction by Noreen Redden to learn about the mechanics of how MacGyver actually works. It’s old, but still applies.
• To see a replay of the recent Webinar, go to MacGyver Technique Introduction
I'll be submitting this to the Tips & Techniques folks for posting there, also.
David
In FOCUS since 1985 - WF 8.009/8.104 Win 8 Outputs: ALL of 'em! Adapters: Sql Server Teradata Oracle